I have a nawk that reads in a log file and outputs a file that matches my search.
IFS=" "
while read record
do
`echo $record | nawk 'BEGIN {
FS=" "
}
{
type_record=substr($0, 1, 1);
if \( type_record == "-" \)
printf \("%s" , $9\) >>"filelist.dat";
else
printf \("\\n"\);
\}
'\`
done < x.log
OFS=$IFS
the file that comes out of the nawk has carriage return on the end of the two lines:
nawk_output - filelist.dat
abc.csvOD
def.csvOD
i want to set the filenames in a variable i.e.
files_found=`cat x.dat`
the problem is that when i call the variable $files_found it shows as this:
abc.csvf.csv
I want it to show:
abc.csv
def.csv
Its the carriage return that is the problem but can any one help??
Scott
November 19, 2009, 4:31am
2
You can use quotes to preserve the newline.
i.e.
echo "$files_found"
thanks for the reply but even with double quotes i get:
abc.csvf.csv
????
aigles
November 19, 2009, 4:44am
4
Try the following awk script :
awk -F '[ \r]*' '
/^-/ { print $9 }
' x.log > filelist.dat
Jean-Pierre.
this code produced a syntax error
bailing out at line 1
aigles
November 19, 2009, 4:59am
6
Works fine on my AIX box.
Try to replace awk by nawk or gawk.
Jean-Pierre.
Hi Jean-Pierre,
replaced with nawk but got this output:
abc.csvdef.csv
I need them seperating, like below?
abc.csv
def.csv
Scott
November 19, 2009, 5:09am
8
In your original code, try adding a space after %s:
printf ("%s " , $9) >>"filelist.dat"
the space does this to filelist.dat:
abc.csv
(space)def.csv
when i set this file to a variable i get:
echo $files_found:
(space)def.csvbc.csv
aigles
November 19, 2009, 5:22am
10
$ cat pablo.sh
awk -F '[ \r]*' '
/^-/ { print $9 }
' x.log > filelist.dat
files_found=$(<filelist.dat)
echo Print without quotes
echo $files_found
echo Print withquotes
echo "$files_found"
$ ./pablo.sh
Print without quotes
abc.csv def.csv
Print withquotes
abc.csv
def.csv
$
Jean-Pierre.
copied everything you have done in your thread Jean Pierre and i get:
with quotes:
abc.csvdef.csv
I am struggling to understand why it is not doing this. I am usin g KSH
aigles
November 19, 2009, 5:41am
12
Please, post the output of the following commands :
$ grep '^-' x.log | od -cx
$ od -cx filelist.dat
Jean-Pierre.
please see below Jean Pierre:
out put from grep '^-' x.log | od -cx:
0000000 - r w x r w x - - - 1 n
2d72 7778 7277 782d 2d2d 2020 2031 206e
0000020 o - u s e r n o - g r o u p
6f2d 7573 6572 2020 6e6f 2d67 726f 7570
0000040 8 3 6 6 6 7 7 S e p 3 0
2020 3833 3636 3637 3720 5365 7020 3330
0000060 1 1 : 5 9 a b c . c s v \r \n
2031 313a 3539 2061 6263 2e63 7376 0d0a
0000100 - r w x r w x - - - 1 n
2d72 7778 7277 782d 2d2d 2020 2031 206e
0000120 o - u s e r n o - g r o u p
6f2d 7573 6572 2020 6e6f 2d67 726f 7570
0000140 5 1 2 O c t 7
2020 2020 2020 3531 3220 4f63 7420 2037
0000160 1 1 : 1 7 d e f . c s v \r \n
2031 313a 3137 2064 6566 2e63 7376 0d0a
0000200
output from od -cx filelist.dat
0000000 a b c . c s v \r d e f . c s v \r
6162 632e 6373 760d 6465 662e 6373 760d
0000020
aigles
November 19, 2009, 6:09am
14
I don't understand why awk is using \r as the record separator.
Try to force the value of the record separator :
awk -F '[ \r]*' -v ORS='\n' '
/^-/ { print $9 }
' x.log > filelist.dat
Jean-Pierre.
Please use CODE tags in your posts.
It worked......
thanks so much Jean Pierre and everyone else that has posted, I will use code tags in the future
thanks