LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   break line into words (http://www.linuxquestions.org/questions/programming-9/break-line-into-words-773156/)

nushki 12-03-2009 05:47 AM

break line into words
 
Hello,

i need to read file line by line and break each line into words(they are numbers separated by space).

file
1 4 5 6 7 15 24 0
2 9 10 11 14 20 28 0
3 12 18 23 0 0 0 0
8 19 21 29 0 0 0 0
13 16 22 0 0 0 0 0
17 25 26 0 0 0 0 0



i tried

cat file | while read $line;do

echo "$1 $2 $3"

done


but it doesn't work.

Could anybody help me please

druuna 12-03-2009 05:55 AM

Hi,

This is one way (many others exist):

sed 's/\([0-9][0-9]\)* /\1\n/g' infile

and another one:

awk 'BEGIN { OFS="\n" } { print $1, $2, $3, $4, $5, $6, $7, $8 }' infile

Hope this helps.

ashok.g 12-03-2009 06:06 AM

You can try this:

Quote:

awk '{for(i=1;i<NF+1;i++)print $i}' filename

ashok.g 12-03-2009 06:11 AM

Quote:

Originally Posted by druuna (Post 3778181)
Hi,

This is one way (many others exist):

sed 's/\([0-9][0-9]\)* /\1\n/g' infile

Hi Drunna, I am interested in your code as well. what does "\1" in your code represent?

druuna 12-03-2009 06:25 AM

Hi,

This sed command makes use of backtracking, which does the following:

All between ( and ) in the search part (\([0-9][0-9]\)) can be represented by 1 in the replace part. If more of these (...) constructs exist the next one is 2 (3, 4 etc). These need to be escaped.

So this command: sed 's/\([0-9][0-9]\)* /\1\n/g' infile looks for numbers followed by a space. Because the found number is between \( and \) in the search part, you can use \1 to use it in the replace part. The \n is there to jump to the next line.

Other example: switching columns.

infile:

1 a
2 b
3 c

sed 's/\([0-9]\) \([a-z]\)/\2 \1/' infile

output:

a 1
b 2
c 3

Hope this clears things up a bit.

ashok.g 12-03-2009 06:46 AM

Ya. Thanks alot.

Telemachos 12-03-2009 07:42 AM

A Perl one-liner:
Code:

perl -lane 'print $_ for @F' file.txt

nushki 12-03-2009 09:41 AM

thank you very much!

nushki 12-03-2009 09:44 AM

thank you very much!!!

nushki 12-03-2009 09:56 AM

i also need to replace all non-first numbers by the first number in each line in file2.

file1
1 4 5 6 7 15 24 0
2 9 10 11 14 20 28 0
3 12 18 23 0 0 0 0
8 19 21 29 0 0 0 0
13 16 22 0 0 0 0 0
17 25 26 0 0 0 0 0

when i find number 4,5,6,7,15 or 24 in file2 i must replace it with 1 in file2.
when i find number 9,10 11 14 20 28 in file2 i must replace it with 2 in file2.
etc
I tried

awk '{for(i=2;i<NF+1;i++) sed -i 's/$i/$1/g' file2}' file1


but it doesn't work either :(.

AnanthaP 12-03-2009 07:39 PM

Quote:

awk '{for(i=2;i<NF+1;i++) .... sed -i 's/$i/$1/g' file2}' file1
When you string together more than i unix command, then you must use piping. Either read up or get your teacher to tell you about it.

From the beginning I got one doubt. The input already seems to be broken into words (8 words per line with standard separator). For the final question, I would just print $1 8 times. This would replace all non first numbers with the first number.

End

Tinkster 12-03-2009 08:07 PM

I know it's late in the game, but there's another one for
the initial request:
Code:

tr " " "\n" < file

nushki 12-04-2009 03:09 AM

i must replace numbers in file2, not in file 1. File2 have a number in each line. by the way i dont have a teacher, i'm working

smeezekitty 12-04-2009 03:42 AM

C code:
Code:

#include <Stdio.h>
unsigned char words[255][255];
int main(){
FILE *f = fopen("<FILENAME>", "rb");
int cw=0,cl=0;
unsigned char ch;
while(!feof(f)){
ch = fgetc(f);
if(ch == ' '){
words[cw][cl] = 0;
cw++; cl=0;
} else {
words[cw][cl] = ch;
cl++;
}
}
for(int i = 0;i < cw;i++){
printf("WORD=%s\n", words[cw]);
}
fclose(f); //Dont want to run out of file handles now do we
}


ghostdog74 12-04-2009 04:13 AM

Quote:

Originally Posted by smeezekitty (Post 3779369)
C code:
Code:

#include <Stdio.h>
unsigned char words[255][255];
int main(){
FILE *f = fopen("<FILENAME>", "rb");
int cw=0,cl=0;
unsigned char ch;
while(!feof(f)){
ch = fgetc(f);
if(ch == ' '){
words[cw][cl] = 0;
cw++; cl=0;
} else {
words[cw][cl] = ch;
cl++;
}
}
for(int i = 0;i < cw;i++){
printf("WORD=%s\n", words[cw]);
}
fclose(f); //Dont want to run out of file handles now do we
}


how is this going to solve OP's problem?


All times are GMT -5. The time now is 12:25 PM.