awk - oneliner vs multiple line .


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - oneliner vs multiple line .
# 1  
Old 01-18-2013
awk - oneliner vs multiple line .

Dear Awk Experts,

I can write oneliner some time but find difficulty writing like a flow of a program in multiple lines,

could you please help how to write awk program , like long awk programs, any help to start with,

For example,
How to write this in multiple line:

Code:
ls -l | awk '{sum+=$5}END{print sum}'


what is equivalent of this in terms of file.awk if want to pass through a file.


Thanks,
# 2  
Old 01-18-2013
Quote:
Originally Posted by rveri
Dear Awk Experts,

I can write oneliner some time but find difficulty writing like a flow of a program in multiple lines,

could you please help how to write awk program , like long awk programs, any help to start with,

For example,
How to write this in multiple line:

Code:
ls -l | awk '{sum+=$5}END{print sum}'


what is equivalent of this in terms of file.awk if want to pass through a file.


Thanks,
I'm not sure what you're asking. If you just want to make the above script easier to read, the following is equivalent to the above script:
Code:
ls -l | awk '
{       sum+=$5}
END {   print sum}'

If you want to put your script in a file instead of typing it every time you run this command, put the following in a file named script.awk:
Code:
{       sum+=$5}
END {   print sum}

and then you can get the same results by typing the command:
Code:
ls -l | awk -f script.awk

If you just want to put all of this in a shell script, create a file named size_sum
containing:
Code:
#!/bin/ksh
ls -l | awk '
{       sum+=$5}
END {   print sum}'

replacing /bin/ksh in the first line with the absolute pathname of the shell you're using to run this script, then enter the commands:
Code:
chmod +x size_sum
if [ ! -d $HOME/bin ]
then    mkdir $HOME/bin
fi
mv size_sum $HOME/bin

and then you can run the script in any directory that is your current working directory by entering the command:
Code:
size_sum

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 01-18-2013
Don, Thanks a lot , your answer makes sense to me , thanks a lot.
# 4  
Old 01-19-2013
You can also split any long line using \ at the end of the line.
Eks
Code:
ls -l | \
awk '
{ sum+=$5}
END \
{ print sum}'


Last edited by Scrutinizer; 01-19-2013 at 06:13 AM.. Reason: moved code tag
# 5  
Old 01-19-2013
Quote:
Originally Posted by Jotne
You can also split any long line using \ at the end of the line.
Eks
Code:
ls -l | \
awk '
{ sum+=$5}
END \
{ print sum}'

The back-slashes are not at all required within quotes.
# 6  
Old 01-19-2013
Its needed, if you split the line.
Code:
ls -l | \
awk '
{ sum+=$5 }
END
{ print sum}'

This gives error awk: cmd. line:3: END blocks must have an action part
# 7  
Old 01-19-2013
Any pattern in awk expects the corresponding action to start on the same line as that of the ending pattern line. If no action is found on that line, the default action (print current record) is taken.

The BEGIN/END blocks are different. They must have some action and those actions (like for other patterns) must start on the BEGIN/END lines.

You may have:
Code:
BEGIN {
blah..blah..blah..
}

or
Code:
END {
blah..blah..blah..}


Last edited by elixir_sinari; 01-19-2013 at 10:17 AM..
This User Gave Thanks to elixir_sinari For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get index values for multiple matches in the same line with awk?

Hi, I know that echo "bob alice robert alice" | awk '{print index($0,"alice")}' 5Will output the index of the first alice match, is there any way to get the index of all matches?, eg: echo "bob alice robert alice" | awk 'unknown magic' 5:18Thanks for your time. (6 Replies)
Discussion started by: chilicuil
6 Replies

2. Shell Programming and Scripting

Replacing multiple line patterns with awk

Hi forum, Can you please help me understand how to look for and replace the below pattern (containing line breaks) and return a new result? Rules: Must match the 3 line pattern and return a 1 line result. I have found solutions with sed, but it seems that sed installed in my system is... (5 Replies)
Discussion started by: demmel
5 Replies

3. UNIX for Dummies Questions & Answers

Using awk to multiple and replace in a specific line

Hi Folks, I have the file in which I need to multiply the content of a line and replace the initial content of that line with the obtained answer. For example if this is my input file file1.txt 2.259314750 xxxxxx 1.962774350 xxxxxx 2.916817290 xxxxxx 1.355026900 ... (4 Replies)
Discussion started by: Madiouma Ndiaye
4 Replies

4. Shell Programming and Scripting

awk multiple line record retrieves only one?

I have a file with a structure like this: Database sequence: some data Database position: number Query: identifier Location: number E-value: number 0 . : . : . STRINGSTRINGSTRINGSTRING |||||||||||||||||||||||||||| ... (5 Replies)
Discussion started by: bdeads
5 Replies

5. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

6. Shell Programming and Scripting

Multiple Line awk search and replace

I have a log file that contains many lines but contains the following line three times: related_pin : "t_bypass"; Here are the 3 occurrences and the two lines after from my file.txt: related_pin : "t_bypass"; sdf_cond : "rstq_b"; timing_sense : negative_unate; ... (6 Replies)
Discussion started by: bobbygb2003
6 Replies

7. Shell Programming and Scripting

(awk?) print multiple lines on one line

I have a log file something like ------- report 1 ------- date 27/01/13 time 08:00 records 1234 ------- report 2------- date 27/01/13 time 08:00 records 1239 ... I'd like output to show as report 1,date 27/01/13,time 08:00,records 1234 report 2,date 27/01/13,time... (6 Replies)
Discussion started by: gefa
6 Replies

8. UNIX for Dummies Questions & Answers

awk, extract last line of multiple files

Hi, I have a directory full of *.txt files. I would like to print the last line of every file to screen. I know you can use FNR for printing the first line of each file, but how do I access the last line of each file? This code doesn't work, it only prints the last line of the last file:BEGIN... (5 Replies)
Discussion started by: Liverpaul09
5 Replies

9. Shell Programming and Scripting

AWK multiple line fields sorting

I have a bash script which takes a log file with each record separated by a #. The records have multiple fields but field $1 is always the date and time. When the script is run it prints the record just fine from oldest to newest. I need to have records print out from newest first. Here is the... (7 Replies)
Discussion started by: numele
7 Replies

10. UNIX for Dummies Questions & Answers

AWK: Multiple patterns per line

Hi there, We have been given a bit of coursework using awk on html pages. Without giving too much away and risking the wrath of the plagerism checks, I can say we need to deal with certain html elements. There may be several of these elements on one line. My question is, if there are more... (1 Reply)
Discussion started by: Plavixo
1 Replies
Login or Register to Ask a Question