Print individual rows.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Print individual rows.
# 1  
Old 07-05-2011
Print individual rows.

Hi all,
I have a file im reading with a for loop using:
Code:
for i in $(cat filename)
do
  etc.
done

the original file is 3 columns, this method prints it out into 3 rows, and i would like to just assign the first to rows (originally first two columns) to separate variables. What would be the easiest way to do this?

I tried to awk and fprint, and sed to print just the second row (line), but this did not work, because the individual lines are not files.

Last edited by Scott; 07-07-2011 at 12:22 PM.. Reason: Code tags
# 2  
Old 07-05-2011
Quote:
Originally Posted by jgrosecl
Hi all,
I have a file im reading with a for loop using:

for i in $(cat filename)
That's a useless use of backticks and useless use of cat. This is dangerous because if the file is large you can easily exceed the size limit of a shell variable. It's also inefficient. Better and safer is:

Code:
while read i
do
    ...
done <filename

'read' is even a shell internal.

Quote:
the original file is 3 columns, this method prints it out into 3 rows, and i would like to just assign the first to rows (originally first two columns) to separate variables. What would be the easiest way to do this?
The while read syntax does this for you too, reading line-by-line into whatever variables you tell it to:

Code:
# 'rest' is necessary because with just a b, given '1 2 3', it will set a=1, b="2 3"
while read a b rest
do
       ...
done < filename

What it splits variables on is controllable by the IFS variable, so if you had a file seperated by , instead of whitespace you could do
Code:
while IFS="," read a b rest
do
       ...
done < filename

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 07-06-2011
Thank you, the while loop is working quite well for me, perhaps you could also help me with the problem I am having now, if its not too much trouble:
Code:
while read a b c
do
  for line in $c
  do
    for i in $b
    do
      wanted_line3=$(grep "$i" /XXXX/XXXX/XXXX/XXXX/*)
      new_name3=$(echo "$wanted_line3" | sed "s/$i/$line/")
      echo $wanted_line3
      echo $new_name3
    done
  done
done< /XXXX/XXXX/XXXX/X.txt

This is turning out quite messy. I am trying to take the argument in each line of b and c, search for lines containing each line of b in a directory, and then replace it with c from the same column row.

Maybe I'm making this a lot harder than it actually needs to be.

Last edited by Scott; 07-07-2011 at 12:21 PM.. Reason: Code tags, please...
# 4  
Old 07-07-2011
Maybe. Could you show the input data you have and the output data you want?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print rows based on separator

For below lines in a file. 68078971 dance routine (jess far back corner) 13902786 368079527 dance routine 13902786 368081191 dance routine (jess far back) 13902786 I am looking for output as below 68078971, "dance routine (jess far back corner)", 13902786... (4 Replies)
Discussion started by: Anjan1
4 Replies

2. Shell Programming and Scripting

Print rows with value zero or less than zero using awk

I tried this. It working fine for small tables. But it is giving values greater than zero in a big file with 8556 columns. Does any one know why ? awk 'FNR == 1{print;next}{for(i=8556;i<=NF;i++) if($i <= 0){print;next}}' input (5 Replies)
Discussion started by: quincyjones
5 Replies

3. Shell Programming and Scripting

Line up Print outs from Separate Rows

A text file has two logs of same event & both logs have to be correlated into a single line before same can be processed further RAW RunningLog Date Month Year Time Event 9 JUN 2013 1932 2 10 JAN 2014 1000 4 Duration Closed ... (10 Replies)
Discussion started by: newageBATMAN
10 Replies

4. UNIX for Dummies Questions & Answers

Print rows with substring in column

Hi I want to print all rows where there is the alphabet N in the 6th column as a substring. Here is what i tried and not working.Please help ! awk ' { if ( $6 == *"N"* ) print $0} ' awk ' { if ( "${6}" == *N* ) print $0} ' awk ' { if( grep -q N <<<$6) print $0} ' (1 Reply)
Discussion started by: newbie83
1 Replies

5. Shell Programming and Scripting

Print various rows in one row

I have this in a file 11.22.33.44 yyyyyyuser With awk/sed, I need this to be output as follows alias server.domain.com='ssh yyyyyyuser@11.22.33.44' (4 Replies)
Discussion started by: anil510
4 Replies

6. UNIX for Dummies Questions & Answers

search one row and print next 9 rows

Hi, I'd like your support on this, my file looks like this: 25 50 200 10 0 1 10 6 12 17 26 24 30 319 10 -0.83 3.31 270.646 12.48 -163.04 30.38 0 0 0 -2.88 0.64 16.06 34.4 33.08 34.4 33.08 34.06 20.48 6.22 -33.39 33.96 -33.5840381 -71.6177759 -163.26 25.59 34.1 -28.8 -5.6 -9.9 0 0 0 0... (3 Replies)
Discussion started by: Gery
3 Replies

7. UNIX for Advanced & Expert Users

Print rows into columns

Hi, I required to print all rows into columns(some comma separated or else) till a pattern match found. Using Solaris UNIX scripting my input is like abc def ghi jkl mno END pqr stu vwx yz 123 END ... ... (7 Replies)
Discussion started by: vikash.rastogi
7 Replies

8. Shell Programming and Scripting

Print rows, having pattern in specific column...

Hello all, :) I have a pattern file some what like this, cd003 cd005 cd007 cd008 and input file like this, abc cd001 cd002 zca bca cd002 cd003 cza cba cd003 cd004 zca bac cd004 cd005 zac cba cd005 cd006 acz acb cd006 cd007 caz cab cd007 ... (25 Replies)
Discussion started by: admax
25 Replies

9. Shell Programming and Scripting

want to print output if u have rows and columns

U have a file File 1 0.3 0.2 0.4 0.3 0.8 0.11 0.22 0.4 0.23 0.45 0.56 0.78 0.98 0.11 0.32 0.2 0.4 0.45 0.54 0.2 0.33 0.44 0.21 0.22 0.98 0.8 0.2 0.34 0.54 0.98 0.12 0.1 0.22 0.32 0.34 0.89 0.22 File 2 rows columns 3 1 2 3 4 2 5 ... (8 Replies)
Discussion started by: cdfd123
8 Replies

10. Shell Programming and Scripting

print selected rows with awk

Hi everybody: Could anybody tell me how I can print from a file a selected rows with awk. In my case I only want print in another file all the rows from NR=8 to NR=2459 and the increment each 8 times. I tried to this: awk '{for (i=8; i=2459; i+=8); NR==i}' file1 > file2 But doesn't... (6 Replies)
Discussion started by: tonet
6 Replies
Login or Register to Ask a Question