Two Loops

Hi please help.

I have a file with two columns.

I want to insert the value of the first column and second column in different sections of a line.

code:

for line in `cat $1`;
for x in `cat $1 |awk '{print $2}'`;
do
do
echo "print $line and then print $x"
done 
done

It works but prints it twice.

Thanks.

please provide a sample input and a desired output to ease the understanding.

Maybe example input and output helps to understand your needs.

Ok so my file would look like this:

red blue
black yellow

output would look like this:

print red and then print blue
print black and then print yellow

thanks!

nawk '{print "print", $1, "and then print", $2}' myFile

is there a way to do this with loops? Because actually my lines are not the same and they have "," and quotes

so example:

"'',165,1293537000,0,'Logs - ABC','$line','*Log*.log','/usr/apps/collectors/$line/log',60,'no',120,'yes',180,'yes'"
"'',198,1295520870,0,'ABC_INBOUND','$line','*$x*','/usr/apps/$line/BR_ABC/ARCHIVE',60,'no',120,'no',180,'yes'"

So basically i have 13 lines with this kind of syntax that I need to print
Some have values for only column 1 and other for both column 1 and 2.
Your syntax was easy but since i have so many charecters to esc it makes it tough. That's why i was trying with a for loop

I don't follow how using loops will simplify the task at hand....
Maybe if you could quote a representative sample of your input data and a desired output (once again - hopefully using code tags)...

I'm sorry if I am not making sense.

Your previous post works well for simple sentences that I would print with no special characters.
but since my lines have lots of commas and quotes and such its was hard

So my thought was if I did a for loop it would just loop through the file and for each line enter it in where I needed it. Then it would loop through the file again and find the second value (second column) and enter it.

so here is what I need done:
file:

red blue
white black

have it print:

"'',165,1293537000,0,'Logs - ABC','$red','*Log*.log','/usr/apps/collectors/$red/log',60,'no',120,'yes',180,'yes'"
"'',198,1295520870,0,'ABC_INBOUND','$red','*$blue*','/usr/apps/$red/BR_ABC/ARCHIVE',60,'no',120,'no',180,'yes'"
"'',165,1293537000,0,'Logs - ABC','$white','*Log*.log','/usr/apps/collectors/$white/log',60,'no',120,'yes',180,'yes'"
"'',198,1295520870,0,'ABC_INBOUND','$white','*$black*','/usr/apps/$white/BR_ABC/ARCHIVE',60,'no',120,'no',180,'yes'"

A Perl solution follows -

$
$
$ # display the contents of the data file "f1"
$
$ cat f1
red blue
white black
$
$
$ # display the contents of the Perl program that processes "f1"
$
$ cat -n f1.pl
   1  #!perl -w
   2
   3  # The template string uses here-doc to take care of multiple lines and single/double quotes
   4  $template = <<END;
   5  "'',165,1293537000,0,'Logs - ABC','\$FIRST','*Log*.log','/usr/apps/collectors/\$FIRST/log',60,'no',120,'yes',180,'yes'"
   6  "'',198,1295520870,0,'ABC_INBOUND','\$FIRST','*\$SECOND*','/usr/apps/\$FIRST/BR_ABC/ARCHIVE',60,'no',120,'no',180,'yes'"
   7  END
   8
   9  # process the file now
  10  open (FH, "f1") or die "Can't open f1: $!";         # open file and assign file handle
  11  while (<FH>) {                                      # while we can read records
  12    chomp;                                            # remove the newline at the end
  13    ($first, $second) = m/^\s*(\w+)\s*(\w+)\s*$/;     # split record into two fields $first and $second, using regex
  14    $line = $template;                                # assign template to the line we are going to work with
  15    $line =~ s/FIRST/$first/g;                        # substitute first and second fields
  16    $line =~ s/SECOND/$second/g;
  17    print $line;                                      # and print the line
  18  }
  19  close (FH) or die "Can't close f1: $!";             # clean up when it's all done
$
$
$ # Now run the Perl program
$
$ perl f1.pl
"'',165,1293537000,0,'Logs - ABC','$red','*Log*.log','/usr/apps/collectors/$red/log',60,'no',120,'yes',180,'yes'"
"'',198,1295520870,0,'ABC_INBOUND','$red','*$blue*','/usr/apps/$red/BR_ABC/ARCHIVE',60,'no',120,'no',180,'yes'"
"'',165,1293537000,0,'Logs - ABC','$white','*Log*.log','/usr/apps/collectors/$white/log',60,'no',120,'yes',180,'yes'"
"'',198,1295520870,0,'ABC_INBOUND','$white','*$black*','/usr/apps/$white/BR_ABC/ARCHIVE',60,'no',120,'no',180,'yes'"
$
$

tyler_durden

The template string will not be interpolated if the here-doc keyword is surrounded by single quotes. So the escape characters for the dollar symbols can be avoided like so -

$
$ cat -n f1.pl
    1  #!perl -w
    2
    3  # The template string uses here-doc to take care of multiple lines and single/double quotes
    4  $template = <<'END';
    5  "'',165,1293537000,0,'Logs - ABC','$FIRST','*Log*.log','/usr/apps/collectors/$FIRST/log',60,'no',120,'yes',180,'yes'"
    6  "'',198,1295520870,0,'ABC_INBOUND','$FIRST','*$SECOND*','/usr/apps/$FIRST/BR_ABC/ARCHIVE',60,'no',120,'no',180,'yes'"
    7  END
    8
    9  # process the file now
   10  open (FH, "f1") or die "Can't open f1: $!";         # open file and assign file handle
   11  while (<FH>) {                                      # while we can read records
   12    chomp;                                            # remove the newline at the end
   13    ($first, $second) = m/^\s*(\w+)\s*(\w+)\s*$/;     # split record into two fields $first and $second, using regex
   14    $line = $template;                                # assign template to the line we are going to work with
   15    $line =~ s/FIRST/$first/g;                        # substitute first and second fields
   16    $line =~ s/SECOND/$second/g;
   17    print $line;                                      # and print the line
   18  }
   19  close (FH) or die "Can't close f1: $!";             # clean up when it's all done
$
$
$ perl f1.pl
"'',165,1293537000,0,'Logs - ABC','$red','*Log*.log','/usr/apps/collectors/$red/log',60,'no',120,'yes',180,'yes'"
"'',198,1295520870,0,'ABC_INBOUND','$red','*$blue*','/usr/apps/$red/BR_ABC/ARCHIVE',60,'no',120,'no',180,'yes'"
"'',165,1293537000,0,'Logs - ABC','$white','*Log*.log','/usr/apps/collectors/$white/log',60,'no',120,'yes',180,'yes'"
"'',198,1295520870,0,'ABC_INBOUND','$white','*$black*','/usr/apps/$white/BR_ABC/ARCHIVE',60,'no',120,'no',180,'yes'"
$
$
$

tyler_durden