for loop with whole line using cat


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting for loop with whole line using cat
# 1  
Old 08-02-2012
Error 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

Quote:
136 1 24 048800 id N4 No_Light
137 1 25 048900 id N4 No_Light
140 1 28 048c00 id N4 No_Light
262 1 38 048e80 id N4 No_Light
263 1 39 048f80 id N4 No_Light
20 2 4 041400 id N4 No_Light


I need the output like below

HTML Code:
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 id N4 No_Light
10.0.0.1,263 1 39 048f80 id N4 No_Light
10.0.0.1,20 2 4 041400 id N4 No_Light
This is my script
Code:
for f in `cat /home/pa039410/switch-ip/all-switch-ip.txt`
do
plink -ssh -v 123@$f -pw 456 switchshow |grep No_Light > /home/pa039410/switch-ip/switchshow-nolight.txt
plink -ssh -v 123@$f -pw 456 switchshow |grep -i online > /home/pa039410/switch-ip/switchshow-online.txt
for g in `cat /home/pa039410/switch-ip/switchshow-nolight.txt`
do
echo $f,$g >> /home/pa039410/switch-ip/switchshow-all-withip.txt 
for h in `cat /home/pa039410/switch-ip/switchshow-nolight.txt`
do
echo $f,$h >> /home/pa039410/switch-ip/switchshow-all-withip.txt
done
done
done

But i getting the output like this

HTML Code:
10.62.75.173,id
10.62.75.173,N4
10.62.75.173,No_Light
10.62.75.173,107
10.62.75.173,11
10.62.75.173,11
10.62.75.173,046b00
10.62.75.173,id
10.62.75.173,N4
10.62.75.173,No_Light
10.62.75.173,109
10.62.75.173,11
10.62.75.173,13
10.62.75.173,046d00
10.62.75.173,id
10.62.75.173,N4
10.62.75.173,No_Light
10.62.75.173,231
# 2  
Old 08-02-2012
Code:
$ cat test.txt
136 1 24 048800 id N4 No_Light
137 1 25 048900 id N4 No_Light
140 1 28 048c00 id N4 No_Light
262 1 38 048e80 id N4 No_Light
263 1 39 048f80 id N4 No_Light
20 2 4 041400 id N4 No_Light

$ awk '{$0="10.0.0.1,"$0}1' test.txt
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 id N4 No_Light
10.0.0.1,263 1 39 048f80 id N4 No_Light
10.0.0.1,20 2 4 041400 id N4 No_Light

# 3  
Old 08-02-2012
you can use awk '{print "10.0.0.1" "," $0}' infile
# 4  
Old 08-02-2012
Code:
cat xx |sed -n 's/.*/10\.0\.0\.1,&/p'

Code:
cat xx |while read line;do echo "10.1.1,${line}" ;done

Code:
cat xx |awk '{print "10.1.1,"$0}'

# 5  
Old 08-02-2012
Quote:
Originally Posted by yanglei_fage
Code:
cat xx |sed -n 's/.*/10\.0\.0\.1,&/p'

Code:
cat xx |while read line;do echo "10.1.1,${line}" ;done

Code:
cat xx |awk '{print "10.1.1,"$0}'

useless use of cat award

Useless Use of Cat Award

awk and sed commands are able to process the file by its own. So dont use cat command
# 6  
Old 08-02-2012
Quote:
Originally Posted by itkamaraj
Code:
$ cat test.txt
136 1 24 048800 id N4 No_Light
137 1 25 048900 id N4 No_Light
140 1 28 048c00 id N4 No_Light
262 1 38 048e80 id N4 No_Light
263 1 39 048f80 id N4 No_Light
20 2 4 041400 id N4 No_Light
 
$ awk '{$0="10.0.0.1,"$0}1' test.txt
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 id N4 No_Light
10.0.0.1,263 1 39 048f80 id N4 No_Light
10.0.0.1,20 2 4 041400 id N4 No_Light


what does '1' here mean, is there a man page?
# 7  
Old 08-02-2012
Code:
$ sed 's/^/10.0.0.1,/' input.txt
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 id N4 No_Light
10.0.0.1,263 1 39 048f80 id N4 No_Light
10.0.0.1,20 2 4 041400 id N4 No_Light

---------- Post updated at 09:12 PM ---------- Previous update was at 09:11 PM ----------

Quote:
Originally Posted by yanglei_fage
what does '1' here mean, is there a man page?
awk evaluates the 1 as true and the prints the entire line by default, including a newline.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 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

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

4. 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

5. 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

6. Shell Programming and Scripting

cat file1 read line-per-line then grep -A 15 lines down in fileb

STEP 1 # Set variable FILE=/tmp/mainfile SEARCHFILE =/tmp/searchfile # THIS IS THE MAIN FILE. cat /tmp/mainfile Interface Ethernet0/0 "outside", is up, line protocol is up Hardware is i82546GB rev03, BW 100 Mbps Full-Duplex(Full-duplex), 100 Mbps(100 Mbps) MAC address... (6 Replies)
Discussion started by: irongeekio
6 Replies

7. Shell Programming and Scripting

cat and loop

Hi I have a simple code that I want to execute. 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 (10 Replies)
Discussion started by: soemac
10 Replies

8. Shell Programming and Scripting

Cat'ing a multiple line file to one line

I am writing a script that is running a loop on one file to obtain records from another file. Using egrep, I am finding matching records in file b, then outputing feilds of both into another file. **************************** filea=this.txt fileb=that.txt cat $filea | while read line do... (1 Reply)
Discussion started by: djsal
1 Replies

9. UNIX for Dummies Questions & Answers

using 'cat' to in 'while read line'

Hi, I'm having some trouble reading a file that was 'cat' through a while loop. Can anyone suggest alternatives? what i do is cat filename|grep *.stuff while read line do echo $line ... and other commands done The cat,grep line seems to work correctly, but the script hangs when i add in... (3 Replies)
Discussion started by: chugger06
3 Replies

10. 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