LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-03-2009, 04:47 AM   #1
nushki
LQ Newbie
 
Registered: Mar 2009
Posts: 26

Rep: Reputation: 0
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
 
Old 12-03-2009, 04:55 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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.

Last edited by druuna; 12-03-2009 at 04:58 AM.
 
1 members found this post helpful.
Old 12-03-2009, 05:06 AM   #3
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Rep: Reputation: 32
You can try this:

Quote:
awk '{for(i=1;i<NF+1;i++)print $i}' filename
 
1 members found this post helpful.
Old 12-03-2009, 05:11 AM   #4
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Rep: Reputation: 32
Quote:
Originally Posted by druuna View Post
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?
 
Old 12-03-2009, 05:25 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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.
 
Old 12-03-2009, 05:46 AM   #6
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Rep: Reputation: 32
Ya. Thanks alot.
 
Old 12-03-2009, 06:42 AM   #7
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
A Perl one-liner:
Code:
perl -lane 'print $_ for @F' file.txt
 
1 members found this post helpful.
Old 12-03-2009, 08:41 AM   #8
nushki
LQ Newbie
 
Registered: Mar 2009
Posts: 26

Original Poster
Rep: Reputation: 0
thank you very much!
 
Old 12-03-2009, 08:44 AM   #9
nushki
LQ Newbie
 
Registered: Mar 2009
Posts: 26

Original Poster
Rep: Reputation: 0
thank you very much!!!
 
Old 12-03-2009, 08:56 AM   #10
nushki
LQ Newbie
 
Registered: Mar 2009
Posts: 26

Original Poster
Rep: Reputation: 0
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 .

Last edited by nushki; 12-03-2009 at 11:49 AM.
 
Old 12-03-2009, 06:39 PM   #11
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 630

Rep: Reputation: 137Reputation: 137
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
 
Old 12-03-2009, 07:07 PM   #12
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,978
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
I know it's late in the game, but there's another one for
the initial request:
Code:
tr " " "\n" < file
 
Old 12-04-2009, 02:09 AM   #13
nushki
LQ Newbie
 
Registered: Mar 2009
Posts: 26

Original Poster
Rep: Reputation: 0
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
 
Old 12-04-2009, 02:42 AM   #14
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,229

Rep: Reputation: 173Reputation: 173
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
}
 
2 members found this post helpful.
Old 12-04-2009, 03:13 AM   #15
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by smeezekitty View Post
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?
 
0 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
line break on bash script ZAMO Linux - General 5 04-24-2010 12:27 PM
Grep words and paste is on the same line say_hi_ravi Programming 7 10-31-2008 06:56 AM
How do I extract characters from several words on a line? MheAd Linux - Newbie 22 06-23-2008 10:22 PM
CVS's line break management zWaR Programming 1 04-16-2007 09:00 PM
BASH: First words in a line JordanH Programming 7 10-24-2004 10:00 AM


All times are GMT -5. The time now is 07:26 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration