cat and loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cat and loop
# 1  
Old 04-04-2008
cat and loop

Hi I have a simple code that I want to execute.
Code:
out=out.txt
for f in `cat list.txt | head -1`; do
	
	echo $f >> $out
	echo "sleep 5" >> $out
done

cat list.txt | head -1

wget -q -O - 'http://test.com:15100/cgi-bin/search


cat out.txt

wget
sleep 5
-q
sleep 5
-O
sleep 5
-
sleep 5
'http://test.com:15100/cgi-bin/search'
sleep 5

The problem is list.txt contains spaces and they are treated as separator.

My ideal output is of course to show

wget -q -O - 'http://test.com:15100/cgi-bin/search'
sleep 5

Any smart way to do this preferably using less code.
# 2  
Old 04-05-2008
One awk line can do your job
Code:
 awk '{if($1=="wget")print $0"\nsleept 5"}' list.txt > out.txt

# 3  
Old 04-05-2008
Not sure why you need a loop

head -1 list.txt > out.txt
echo sleep 5 >> out.txt
# 4  
Old 04-05-2008
Quote:
Originally Posted by soemac
Any smart way to do this preferably using less code.

Code:
IFS= read -r f < list.txt
printf "%s\n" "$f" > "$out"

# 5  
Old 04-05-2008
Just to deviate as little as possible from your original,

Code:
head -1 list.txt >>$out   # yes, the cat is completely meaningless
echo sleep 5 >>$out

If you actually need to get the output into backticks, take care to quote properly.

Code:
string_with_spaces="`head -1 list.txt`"

# 6  
Old 04-05-2008
Quote:
Originally Posted by era
Just to deviate as little as possible from your original,

Code:
head -1 list.txt >>$out   # yes, the cat is completely meaningless


And head is an unnecessary external command. It is much faster to use read to get the first line from a file.
Quote:
Code:
echo sleep 5 >>$out

If you actually need to get the output into backticks, take care to quote properly.

Code:
string_with_spaces="`head -1 list.txt`"


The quotes are unnecessary.

It is not a string with spaces that is being assigned to the variable, it is the output of command substitution, which is not subject to word splitting by the shell.
# 7  
Old 04-05-2008
Granted, the quotes are unnecessary in this context -- I didn't want to go back to the useless for loop just to explain the issue, so I made a rather inexact statement instead. Humor me.

Looks like ohagar already posted an identical suggestion -- sorry, missed that somehow before.

Anyway, I believe there is an option in wget itself to wait a specified number of seconds between retrieving two URLs, so the whole question seems kind of ... intriguing.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Issue with cat command on a for loop

Good day to all, I'd like to ask for your advice with regards to this. Scenario : I have here a file named TEST.tmp wherein the value inside is below; "ONE|TWO|FIVE|THREE|FOUR|SIX~SEVEN~EIGHT" "NINE" But when I'm trying to use this in a simple command like; for TESTING in $(cat... (4 Replies)
Discussion started by: asdfghjkl
4 Replies

2. Shell Programming and Scripting

Problem while displaying(cat) file content inside telnet loop .

Hi Team, Not getting the file output inside my email which i am sending from unix box. . Please refer the below code : #!/bin/sh { sleep 5 echo ehlo 10.56.185.13 sleep 3 echo mail from: oraairtel@CNDBMUREAPZP02.localdomain sleep 3 echo rcpt to: saurabhtripathi@anniksystems.com... (1 Reply)
Discussion started by: tripathi1990
1 Replies

3. Shell Programming and Scripting

for loop with whole line using cat

Hi all, I need to create loop script to read full line and append a variable to each line. cat file I need the output like below 10.0.0.1,136 1 24 048800 id N4 No_Light 10.0.0.1,137 1 25 048900 id N4 No_Light 10.0.0.1,140 1 28 048c00 id N4 No_Light 10.0.0.1,262 1 38 048e80... (13 Replies)
Discussion started by: ranjancom2000
13 Replies

4. Shell Programming and Scripting

Loop through hosts file - cat /etc/resolv.conf

Hello... I am trying to loop through my hosts file that contains 100+ servers to check or update the nameservers on them... My while loop is breaking after the first server responds... #!/bin/bash while read line; do a=( $(echo $line | tr " " "\n") ) if }" != "" ] && }" != "#" ] &&... (1 Reply)
Discussion started by: CompSCI
1 Replies

5. Shell Programming and Scripting

simple for loop/cat issue

ok.. so problem is: I have a file that reads: cat 123 1 and 2 3 and 4 5 and 6 I was using for loops to run through this information. Code: for i in `cat 123` do echo $i done shouldn't the output come as 1 and 2 (3 Replies)
Discussion started by: foal_11
3 Replies

6. Shell Programming and Scripting

cat in the command line doesn't match cat in the script

Hello, So I sorted my file as I was supposed to: sort -n -r -k 2 -k 1 file1 | uniq > file2 and when I wrote > cat file2 in the command line, I got what I was expecting, but in the script itself ... sort -n -r -k 2 -k 1 averages | uniq > temp cat file2 It wrote a whole... (21 Replies)
Discussion started by: shira
21 Replies

7. Shell Programming and Scripting

for i in `cat myname.txt` && for y in `cat yourname.txt`

cat myname.txt John Doe I John Doe II John Doe III ----------------------------------------------------------------------- for i in `cat myname.txt` do echo This is my name: $i >> thi.is.my.name.txt done ----------------------------------------------------------------------- cat... (1 Reply)
Discussion started by: danimad
1 Replies

8. UNIX for Dummies Questions & Answers

Difference between cat , cat > , cat >> and touch !!!

Hi Can anybody tell the difference between Difference between cat , cat > , cat >> and touch command in UNIX? Thanks (6 Replies)
Discussion started by: skyineyes
6 Replies

9. UNIX for Dummies Questions & Answers

Using cat command in a for loop

If I have a data file containing entries like-> abc abc:123 and I use a for loop: for I in `cat data-file` do echo $I done the output would contain 2 lines -> abc.... and abc:123 but I want it to be on only one line. How can I do this? thanks (1 Reply)
Discussion started by: sleepster
1 Replies
Login or Register to Ask a Question