How to Obtain Second and Fourth Word from Each Line in a File

How to obtain second and fourth word from each line in a file

Here is another efficient way to do that..

dictNames = {}
with open('Sentences.dat', 'r') as f:
for line in f:
words = line.split()
dictNames[words[1]] = words[3]

Only print first and second word of each line to output with sed

In GNU sed

sed -E '/^\s*(#.*)?$/d; s/^\s*(\S+)\s+(\S+).*/\1 \2/' pattern.txt

Update after the comments:

sed -E '/^\s*(#.*)?$/d; s/^\s*(\S+)\s+("[^"]*"|\S+).*/\1 \2/' pattern.txt

Extract The Second Word In A Line From A Text File

2nd word

echo '*   CRISTOBAL  AL042014  08/05/14  12  UTC   *' | awk  '{print $2}'

will give you CRISTOBAL

echo 'testing only' | awk  '{print $2}'

will give you only

You may have to modify this if the structure of the line changes.

2nd word from lines in a text file

If your text file contains the following two sentences

this is a test
* CRISTOBAL AL042014 08/05/14 12 UTC *

running awk '{print $2}' filename.txt will return

is
CRISTOBAL

2nd character

echo 'testing only' | cut -c 2

This will give you e, which is the 2nd character, and you may have to modify this so suit your needs also.

Using awk to find the 2nd, 3rd and 4th words in a string

You need the print command to produce output. Also, put commas between them to get spaces between the words.

And it should be inside $(), not $(()). The latter is for evaluating arithmetic expressions, not command substitution.

threewords=$(echo $line | awk '{print $2, $3, $4}')

C# Read every second word from txt file

I want to believe that spaces separate words in your text file. You can do it like so:

text.Split(' ')[1]

If there are many lines in your text file, you can loop through each line and get the second word like this and you also check if the words on each line are greater than one after the split to avoid System.IndexOutOfRangeException exception:

        foreach(var line in System.IO.File.ReadAllText(@"C:\test.txt"))
{
if(line.Split(' ').Count() > 1)
secondWord = line.Split(' ')[1];
}

grep a file and print only n'th word in the line

Assuming your data was in a file called test:

cat test | grep CPU0 | tail -1 | awk '{ printf("%d", $3) }'

grep CPU0 test | tail -1 | awk '{ printf("%d", $3) }' - condensed

awk ' /CPU0/ {a=$3} END{ printf("%d", a) }' test - more condensed

What it does:

  • cat will output all lines in test file
  • grep CPU0 will only output those lines that contain CPU0
  • tail -1 will give the last line from grep's output
  • awk will split []# 0-CPU0 38.8%, SM: 1/4, ovl: 62 by space(s)
  • first item is []#, second is 0-CPU0, third is 38.8%
  • awk's printf %d will give you just 38

C++ code to get line of file and read the second word of the line?

You wanted a comment of each line

// function that returns a word from 'line' with position 'index'
// note that this is not a zero based index, first word is 1,
// second is 2 etc ..
string strWord(int index, string line)
{
int count = 0; // number of read words
string word; // the resulting word
for (int i = 0 ; i < line.length(); i++) { // iterate over all characters in 'line'
if (line[i] == ' ') { // if this character is a space we might be done reading a word from 'line'
if (line[i+1] != ' ') { // next character is not a space, so we are done reading a word
count++; // increase number of read words
if (count == index) { // was this the word we were looking for?
return word; // yes it was, so return it
}
word =""; // nope it wasn't .. so reset word and start over with the next one in 'line'
}
}
else { // not a space .. so append the character to 'word'
word += line[i];
}
}
}


int main( ) // main function of the program, execution starts here
{
ifstream inFile; // construct input file stream object
inFile.open("new.txt"); // associate the stream with file named "new.txt"
string line, id; //

cout << "Enter id : "; // write "Enter id :" to console
cin >> id; // read input from console and put the result in 'id'

while (!inFile.eof()) { // do the following as long as there is something to read from the file
getline(inFile, line); // read a line from the file and put the value into 'line'
if (strWord(1, line) == id) { // if the first word in 'line' equals 'id' ..
cout << strWord(2, line) << endl; // prints the second word in 'line'
break; // exits the while loop
}
}

system("pause"); // pause the program (should be avoided)
}

Trim a string to get second word, third word and fourth word in java?

String trimFirstWord(String s) {
return s.contains(" ") ? s.substring(s.indexOf(' ')).trim() : "";
}


Related Topics



Leave a reply



Submit