awk print expression


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk print expression
# 1  
Old 07-28-2013
awk print expression

Hi All,

I have a doubt in awk print exp.

Where in some awk commands have seen a digit 1 appended at the end of the awk ,didnt remember the command .

like ..
Code:
cat file |awk '{print }1'

Could some one help in understanding these cases where we use them.


Regards,
Ganesh,

Last edited by Franklin52; 07-29-2013 at 03:49 AM.. Reason: Please use code tags
# 2  
Old 07-28-2013
First:
No need to cat the file to awk
correct awk '{print }1' file
This will print every line two times.

awk has this format
test {action}
this can be repeated
test {action} test {action} test {action}

It this example awk '{print }1' it starts with an action {print }. Since there are no test, it will be true and line will be printed
Next there is an 1 test without action. The default action is print so this will also print the line.

{print} gives same result as 1
So awk '{print }1' could be written awk '{print;print }' or awk '{print }{print}'
This User Gave Thanks to Jotne For This Post:
# 3  
Old 07-28-2013
Thanku Jotne,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep command to search a regular expression in a line an only print the string after the match

Hello, one step in a shell script i am writing, involves Grep command to search a regular expression in a line an only print the string after the match an example line is below /logs/GRAS/LGT/applogs/lgt-2016-08-24/2016-08-24.8.log.zip:2016-08-24 19:12:48,602 ERROR... (9 Replies)
Discussion started by: Ramneekgupta91
9 Replies

2. Shell Programming and Scripting

How to print the extended regular expression ?

Hello, How to print the field separator in awk? please see the following code: cat a.txt a 1s 2s 3s 4s b 2s 4s $ awk 'BEGIN{FS==" "} {print $2 $3 }' te 1s2s 2s4s I want to get the following output : 1s 2s 2s 4s How to realize this ? $ cat te a 1s,,2s 3s ... (11 Replies)
Discussion started by: 915086731
11 Replies

3. Shell Programming and Scripting

Would like to print 3 lines after a regular expression is found in the logfile

I would like to print 3 lines after a regular expression is found in the logfile. I'm using the following code: grep -n "$reg_exp" file.txt |while read LINE ;do i=$(echo $LINE |cut -d':' -f1 ) ;sed -n "$i,$(($i+3))p" file.txt ;done The above code things works fine,but sometimes gives erroneous... (3 Replies)
Discussion started by: joachimshaun
3 Replies

4. Solaris

sed command to print lines after expression

Hi guys. I need a sed command to print like 10 lines after a regular expression is found in the log. Can anyone help me out. Thanks ---------- Post updated at 10:52 AM ---------- Previous update was at 10:34 AM ---------- never mind. I just did the search bewteen two expressions. (1 Reply)
Discussion started by: jamie_collins
1 Replies

5. Shell Programming and Scripting

search for an expression in a file and print the 3 lines above it

Hi, I have a file like this comment.txt 1.img 2.img 3.img OK x.img y.img z.img not ok 1.img 2.img 3.img bad 1.img 2.img 3.img (7 Replies)
Discussion started by: avatar_007
7 Replies

6. Shell Programming and Scripting

find expression with awk in only one column, and if it fits, print whole column

Hi. How do I find an expression with awk in only one column, and if it fits, then print that whole column. 1 apple oranges 2 bannanas pears 3 cats dogs 4 hesaid shesaid echo "which number:" read NUMBER (user inputs number 2 for this example) awk " /$NUMBER/ {field to search is field... (2 Replies)
Discussion started by: glev2005
2 Replies

7. UNIX for Dummies Questions & Answers

Awk inside Awk expression

Hi, It can be used awk inside other Awk?. I need to get another text processing while other text process. Thank you. (2 Replies)
Discussion started by: pepeli30
2 Replies

8. Shell Programming and Scripting

Regular expression in grep -E | awk print

Hi All, I have file.txt with contents like this: random text To: recipient@email.co.uk <HTML>S7randomtext more random text random text To: recip@smtpemail.com <HTML>E5randomtext more random text random text I need the output to look like this: 1,,,1,S7 1,,,1,E5 My code so... (9 Replies)
Discussion started by: terry2009
9 Replies

9. Shell Programming and Scripting

Regular expression in AWK

Hello world, I was wondering if there is a nicer way to write the following code (in AWK): awk ' FNR==NR&&$1~/^m$/{tok1=1} FNR==NR&&$1~/^m10$/{tok1=1} ' my_file In fact, it looks for m2, m4, m6, m8 and m10 and then return a positive flag. The problem is how to define 10 thanks... (3 Replies)
Discussion started by: jolecanard
3 Replies

10. Shell Programming and Scripting

How do I get awk to print a " in it's print part?

The line is simple, use " '{ print $1"]"$2"\"$3THE " NEEDS TO GO HERE$4 }' I've tried \", "\, ^" and '"" but none of it works. What am I missing? Putting in the [ between $1 and $2 works fine, I just need to do the same with a ". Thanks. (2 Replies)
Discussion started by: LordJezo
2 Replies
Login or Register to Ask a Question