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
# 8  
Old 08-02-2012
Quote:
Originally Posted by yanglei_fage
what does '1' here mean, is there a man page?
The 1, either by itself or after an outer code-block, is an implied 'print' statement. Whenever that expression is true, it prints the current line buffer. 1 is always true, so it always prints. The expression can be more complicated than that however.

This means awk '1' is equivalent to cat, awk '/asdf/' would be equivalent to grep asdf, awk '/asdf/ && !/qwerty/' would be equivalent to grep asdf | grep -v qwerty, etc, etc. You can make the expressions as complicated as you want, even including variables and the like, allowing you to reduce long pipe chains into one awk statement, or easily do complicated things that would be bothersome or impossible in grep or sed.

You can check for the presence of a man page via 'man awk'. The linux version of the manual page is quite good I've found.
# 9  
Old 08-02-2012
it is working but ima not able to print with variable
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
awk '{print "$f" "," $0}' /home/pa039410/switch-ip/switchshow-nolight.txt >> /home/pa039410/switch-ip/switchshow-all-withname.txt
awk '{print "$f" "," $0}' /home/pa039410/switch-ip/switchshow-online.txt >> /home/pa039410/switch-ip/switchshow-all-withname.txt 
done

Now iam getting output

Quote:

$f, 2 1 2 040200 id N8 Online F-Port 50:00:09:74:08:1b:b9:10
$f, 3 1 3 040300 id N4 Online F-Port 50:06:04:8c:52:a9:a6:d2
$f, 4 1 4 040400 id N4 Online F-Port 50:06:04:8c:52:a9:a6:c6
$f, 5 1 5 040500 id N4 Online F-Port 50:06:01:60:3b:20:20:60
$f, 6 1 6 040600 id N4 Online F-Port 21:00:00:1b:32:10:23:13
$f, 7 1 7 040700 id N4 Online F-Port 21:00:00:1b:32:11:eb:70
$f, 8 1 8 040800 id N4 Online F-Port 21:00:00:1b:32:89:4e:c6
$f, 9 1 9 040900 id N4 Online F-Port 21:00:00:1b:32:9f:ca:26
$f, 10 1 10 040a00 id N2 Online F-Port 21:00:00:e0:8b:06:40:bf
# 10  
Old 08-02-2012
Variables do not work inside single quotes. Neither does awk share variables with the the shell.

The best way to put a variable into awk is like

awk '{print VAR "," $0}' VAR="$f" filename
# 11  
Old 08-02-2012
There are many fundamental mistakes in your script. See useless use of backticks for starters. Also, you don't need to reopen the same file 99,000 times to print 99,000 things. You don't need to ssh into a host twice to run two commands, either.

You shouldn't need plink on UNIX, either.
# 12  
Old 08-02-2012
Quote:
Originally Posted by itkamaraj
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 ----------



awk evaluates the 1 as true and the prints the entire line by default, including a newline.
we can use below directly, why we have "1" usage, can you give a good example for this usage?
Code:
awk '$0="10.0.0.1,"$0' test.txt

# 13  
Old 08-02-2012
You are still using that syntax, you just don't know it. Like C, its okay to have an = inside an expression.

Your expression is $0="10.0.0.1,"$0

If the result is anything except 0 or "", that will be considered true, and cause the line to be printed.
# 14  
Old 08-02-2012
Instead of connecting to the remote machine twice, why not redirect output locally, then process it?
You separate into switchshow-nolight.txt and switchshow-online.txt but then merge them. Do you need these two files or just the final output?

Code:
while read host; do
	plink -ssh -v 123@"$host" -pw 456 switchshow | awk -v "host=$host" '
		index($0,"No_Light") { print host "," $0 > "/home/pa039410/switch-ip/switchshow-nolight.txt"; print }
		index(tolower($0),"online") { print host "," $0 > "/home/pa039410/switch-ip/switchshow-online.txt"; print }' > /home/pa039410/switch-ip/switchshow-all-withname.txt
done < /home/pa039410/switch-ip/all-switch-ip.txt

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