Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to insert a new line character inside a loop 2

Status
Not open for further replies.

gringomike

Technical User
Aug 6, 2003
148
GB
Hi all,

I've written the following (extremely untidy!) section of code that generates a csv file;


for line in `find /path1 -name "file1.txt" | sort `
do
echo $line >> ./list
done

sort ./list > ./list2

for line in `cat ./list2`
do
ls -1 $line | awk -F/ '{print $3}' >> /path/to/file/a.txt
cat $line >> /path/to/file/a.txt
# cat a.txt | sed -e 's/^some_text=//' -e 's/^more_text="//' -e 's/"//' -e '/^$/d' -e 's/^[ \]*//' -e '1d' -e '$d' > x.txt
cat a.txt | sed -e 's/^some_text=//' -e 's/^more_text="//' -e 's/"//' -e '/^$/d' -e 's/^[ \]*//' |awk -F/ -f search.awk > z.txt

done


The problem I have is that the script doesn't write on a new line of the file z.txt each time it cycles through this loop.

z.txt contains data like this;

text1,text2,text3,text4,text5,text6,text7,text8,text9,text10,text11,text12,text13,text14,text15,

I need it to look like this;

text1,text2,text3,text4,text5,text6,
text7,text8,text9,
text10,text11,text12,text13,text14,text15,

i.e - every time it cycles through the loop, I need the output to be written to a new line.

My search.awk file looks like this (thanks vgersh99!);

BEGIN {
FS="(\\|)|(/)"
OFS=","
}
{
for(i=1; i <= NF; i++)
if (match($i, /.*[^0-9].*/) && !match($i, /^ *$/))
printf("%s%s", $i, OFS)
}

I've tried inserting a printf "\n" in various places but have so far had no success.

If anybody has any ideas about how to get round this I would be extremely grateful!

Thanks

GM
 
in the line:

printf("%s%s", $i, OFS)

add \n in the following way:

printf("%s%s\n", $i, OFS)
 
opps, sorry, I misunderstood the question.

Anyway, add the following line to your awk script:

Code:
END { printf"\n" }

Cheers.
 
Hi Chacalinc,

Thanks for the reply.

I've tried that already!

It formats z.txt to look like this;

text1,
text2,
text3,
text4,
text5,
...

There may be (and usually is) several lines of text in each occurance of file1.txt and I need these all to be on the same line.

Any other ideas?

 
sorry, but which post did you use already?

the printf("%s%s\n", $i, OFS) or the END { printf"\n" }?
 
Hi Chacalinc,

I had already tried printf("%s%s\n", $i, OFS) however adding END { printf"\n" } to the end of search.awk doesn't give me the desired output either.

It generates the same as it did before but inserts a blank line at the bottom of the z.txt file.

text1,text2,text3,text4,text5,text6,text7,text8,text9,text10,text11,text12,text13,text14,text15

Just to make sure I've done the right thing, search.awk now looks like this;

BEGIN {
FS="(\\|)|(/)"
OFS=","
}
{
for(i=1; i <= NF; i++)
if (match($i, /.*[^0-9].*/) && !match($i, /^ *$/))
printf("%s%s", $i, OFS)
}
END { printf"\n" }


Thanks for your help!

GM

 
Ok, so if

i.e - every time it cycles through the loop, I need the output to be written to a new line.

I guess you are talking about the z.txt file. Add a printf to the loop:

Code:
for line in `cat ./list2`
do
[red]awk '{ printf("\n") }' >> z.txt[/red]
ls -1 $line | awk -F/ '{print $3}' >> /path/to/file/a.txt
cat $line >> /path/to/file/a.txt
# cat a.txt | sed -e 's/^some_text=//' -e 's/^more_text="//' -e 's/"//' -e  '/^$/d' -e 's/^[ \]*//' -e '1d' -e '$d' > x.txt
cat a.txt | sed -e 's/^some_text=//' -e 's/^more_text="//' -e 's/"//' -e  '/^$/d' -e 's/^[ \]*//' |awk -F/ -f search.awk > z.txt

done

just a question, if you have a loop, why z.txt is created on each loop?

cat a.txt | sed -e 's/^some_text=//' -e 's/^more_text="//' -e 's/"//' -e '/^$/d' -e 's/^[ \]*//' |awk -F/ -f search.awk[red] > [/red]z.txt
 
hmm.. just a printf is necessary, awk is not necessary:

printf "\n" directly.
 
Hi Chacalinc,

We're almost there!

Thanks for your help so far and for pointing out the problem with z.txt.

Adding the printf "\n" >> z.txt line at the top of the loop (and appending to z.txt rather than recreating it everytime the script cycles through the loop) has made a difference but it's still not quite right.

It now produces the following output;

text1,text2,text3,text4,text5,text6,
text1,text2,text3,text4,text5,text6,text7,text8,text9,

Basically, the first line looks fine but it is then repeated on the line below and subsequent lines are appended to it.

Is this more likely to be a problem with my loop than with the insertion of a new line character?

Thanks alot for your help.

GM


 
Perhaps this ?
ls -1 $line | awk -F/ '{print $3}' [red]>[/red] /path/to/file/a.txt

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Excellent!

Thanks to you both for your help - it's working now!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top