need for loop to include fields as one line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need for loop to include fields as one line
# 1  
Old 03-23-2007
need for loop to include fields as one line

another question that could possibly be answered with awk...

Code:
#cat filename
ab   cd   ef:ghi:jk  lm:nop  qrs
ab   cd   ef:ghi:jk  lm:nop  qrs
ab   cd   ef:ghi:jk  lm:nop  qrs
ab   cd   ef:ghi:jk  lm:nop  qrs

# for x in `awk 'sub($1" +"$2" +","",$0) ' filename`; do echo $x; done
ef:ghi:jk  
lm:nop
qrs
ef:ghi:jk  
lm:nop
qrs
ef:ghi:jk  
lm:nop
qrs
ef:ghi:jk  
lm:nop
qrs

Is there awk syntax to include all fields on one line so the for loop reads 4 lines and not 12?

IE


Code:
# for x in `awk 'sub($1" +"$2" +","",$0) ' filename`; do echo $x; done
ef:ghi:jk  lm:nop  qrs
ef:ghi:jk  lm:nop  qrs
ef:ghi:jk  lm:nop  qrs
ef:ghi:jk  lm:nop  qrs


Last edited by prkfriryce; 03-23-2007 at 12:01 PM..
# 2  
Old 03-23-2007
Quote:
Is there awk syntax to include all fields on one line so the for loop reads 4 lines and not 12?
awk reads the line based on the record seperator,

just read and prints the line
Code:
awk '{ print }' file


Sorry if I have not understood correctly!

Could you please post the expected output ? Smilie
# 3  
Old 03-23-2007
Expected/desired output:

# for x in `awk 'sub($1" +"$2" +","",$0) ' filename`; do echo $x; done
ef:ghi:jk lm:nop qrs
ef:ghi:jk lm:nop qrs
ef:ghi:jk lm:nop qrs
ef:ghi:jk lm:nop qrs

the 'for loop' separates each awk line into three variables, so instead of 4 lines, there are 12:

actual output:

# for x in `awk 'sub($1" +"$2" +","",$0) ' filename`; do echo $x; done
ef:ghi:jk
lm:nop
qrs
ef:ghi:jk
lm:nop
qrs
ef:ghi:jk
lm:nop
qrs
ef:ghi:jk
lm:nop
qrs
# 4  
Old 03-23-2007
Code:
awk -F " " '{ print $3, $4, $5 }' filename

or

Code:
awk -F " " '{ for ( i=3; i<=NF; i++ ) { printf "%s ", $i } printf "\n" }' filename


Last edited by matrixmadhan; 03-23-2007 at 12:19 PM..
# 5  
Old 03-23-2007
Quote:
Originally Posted by matrixmadhan
Code:
awk -F " " '{ print $3, $4, $5 }' filename

or

Code:
awk -F " " '{ for ( i=3; i<=NF; i++ ) { printf "%s ", $i } printf "\n" }' filename

both yield the same result:

ef:ghi:jk
lm:nop
qrs
ef:ghi:jk
lm:nop
qrs
ef:ghi:jk
lm:nop
qrs
ef:ghi:jk
lm:nop
qrs

Regardless of the awk output, the 'for loop' logic still interprets each awk oupt line as a three variables. I would liek to set each awk output line as a string to be able to parse in a later function
# 6  
Old 03-23-2007
you don't use the for loop anymore.
just execute either those 2 awk's from provided by matrixmadhan will do.
# 7  
Old 03-23-2007
Quote:
Originally Posted by ghostdog74
you don't use the for loop anymore.
just execute either those 2 awk's from provided by matrixmadhan will do.
i still need to perform an action for each of those lines. IE

for each line awk outputs, do action A,B,C.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regex to include up to blank line.

Hi guys I am trying to figure out how to match a pattern with a regex up to a full blank line. I will show you what I mean with this example: example A movie name: ted movie name: TMNT movie name: Jinxed example B movie names: Gravity Faster Turbo song titles: dont hello problem (8 Replies)
Discussion started by: acoding
8 Replies

2. Shell Programming and Scripting

Mention file include on command line: awk

Hi All: I need your help please. I have a include file *.awk , and I would want to know if it's there a option's awk where I can use this file include, for example: awk -f decodifica_bitmap.awk cadena_hex.txt -e /home/snh/Awk/include/funciones.awk > sal.out it give error: awk:... (1 Reply)
Discussion started by: solaris21
1 Replies

3. Shell Programming and Scripting

Use less pipe for grep or awk sed to print the line not include xx yy zz

cat file |grep -v "xx" | grep -v "yy" |grep -v "zz" (3 Replies)
Discussion started by: yanglei_fage
3 Replies

4. Shell Programming and Scripting

Comparison of fields then increment a counter reading line by line in a file

Hi, i have a scenario were i should compare a few fields from each line then increment a variable based on that. Example file 989878|8999|Y|0|Y|N|V 989878|8999|Y|0|N|N|V 989878|8999|Y|2344|Y|N|V i have 3 conditions to check and increment a variable on every line condition 1 if ( $3... (4 Replies)
Discussion started by: selvankj
4 Replies

5. Shell Programming and Scripting

How to parse fixed-width columns which may include empty fields?

I am trying to selectively display several columns from a db2 query, which gives me a fixed-width output (partial output listed here): --------- -------------------------- ------------ ------ 000 0000000000198012 702 29 000 0000000000198013 ... (9 Replies)
Discussion started by: ahsh79
9 Replies

6. Shell Programming and Scripting

grep a string with include next 100 line

Hi, I have a log file which has number of lines from that i have to grep a string like this 03:22:19,287 while greping this string i have to grep next lines to the maximum of finding the word (MORE...) the stack is like this ************* 22/11/2011 03:22:19,287 232290646 ERROR... (2 Replies)
Discussion started by: thelakbe
2 Replies

7. Shell Programming and Scripting

include NULLs in line length check

Hello, I am checking the length of each line of a fixed length file and making sure all lines are 161 length. My problem is that some files contain null characters which gets stripped out of my echo. How do I have the NULLs included in my check? (and I cannot replace or sub the NULL values with... (10 Replies)
Discussion started by: ironmix
10 Replies

8. Shell Programming and Scripting

Include Line Before Pattern Using Sed / Awk

Hi, I have a sql file that runs something like this vi Test.sql REVOKE EXECUTE ON DEMO_USER.SQC_SAMP FROM PUBLIC; REVOKE EXECUTE ON DEMO_USER.SQC_SAMP FROM DEMO_READ; REVOKE SELECT ON DEMO_USER.DEMO_NOMINEE_TEST FROM DEMO_READ; REVOKE EXECUTE ON DEMO_USER.SQC_SAMP FROM... (3 Replies)
Discussion started by: rajan_san
3 Replies

9. Shell Programming and Scripting

Compare multiple fields in file1 to file2 and print line and next line

Hello, I have two files that I need to compare and print out the line from file2 that has the first 6 fields matching the first 6 fields in file1. Complicating this are the following restrictions 1. file1 is only a few thousand lines at most and file2 is greater than 2 million 2. I need to... (7 Replies)
Discussion started by: gillesc_mac
7 Replies

10. Shell Programming and Scripting

how to include field separator if there are blank fields?

Hi, I have the following data in the format as shown (note: there are more than 1 blank spaces between each field and the spaces are not uniform, meaning there can be one blank space between field1 and field2 and 3 spaces between field3 and field4, in this example, # are the spaces in between... (19 Replies)
Discussion started by: ReV
19 Replies
Login or Register to Ask a Question