combining awk and sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting combining awk and sed
# 1  
Old 02-21-2011
Lightbulb combining awk and sed

Hi experts,
I have a requirement, In which I need to display the first and last line of a zip file where the line starts with "L". I've writen the code like below using sed and awk.
Code:
gunzip -c 20110203.1104.gz | awk '$1 ~ "^L" {print substr($0,178,15)}' | sed -n '1p;$p'

Is it possible to do it awk only ...? or my commands is good one and don't need any changes ..

Waiting for your suggestions.
Thanks
senthil
# 2  
Old 02-21-2011
I feel your sed part alone will work gr8, it is not.

gunzip -c 20110203.1104.gz | sed -n '1p;$p'
# 3  
Old 02-21-2011
No, I need only lines which starts with L, and i need to substring the line from 178 to 15 character,

thanks
# 4  
Old 02-21-2011
Try:
Code:
gunzip -c 20110203.1104.gz | 
awk '/^L/{print substr($0,178,15);f=1} f && /^L/{s=$0} END{print substr(s,178,15)} '

# 5  
Old 02-21-2011
It retruned complete file

Code:
 
$>^L/{print substr($0,178,15);f=1} f && /^L/{s=$0} END{print substr(s,178,15)}' | wc -l <
1022
$>history | tail -2
147 gunzip -c 20110203.1104.gz | awk '/^L/{print substr($0,178,15);f=1} f && /^L/{s=$0} END{print substr(s,178,15)}' | wc -l
148 history | tail -2
$>zgrep -c  "^L" 20110203.1104.gz 
1021

Is it anything worng i'm trying

Last edited by senthil.ak; 02-21-2011 at 08:14 AM.. Reason: code tag missing
# 6  
Old 02-21-2011
Sorry, didn't test the command, try this one:
Code:
gunzip -c 20110203.1104.gz | 
awk 'f && /^L/{s=$0;next} /^L/{print substr($0,178,15);f=1} END{print substr(s,178,15)}'

This User Gave Thanks to Franklin52 For This Post:
# 7  
Old 02-21-2011
Great it works perfectly.

Code:
 
$>gunzip -c 20110203.1104.gz | awk 'f && /^L/{s=$0;next} /^L/{print substr($0,178,15);f=1} END{print substr(s,178,15)}' 
000000048973373
000000048974393

Could please explain the command for me.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help using combining variables with sed command (RHEL 7)

Here is the whole script, very simple, but I am just learning ROK_NO=$1 RPT=/tmp/test sed -E '/^SELECT/ s/(.{23}).{8}/\1'"$ROK_NO"' /' $RPT echo $RPT When I run this I get $ bash rok.sh 2388085 : No such file or directory /tmp/test When I type the command in console, it works... (3 Replies)
Discussion started by: isey78
3 Replies

2. Shell Programming and Scripting

Combining sed actions

Let's say I have an input file looking like: ID1 1 5 6 8 ID2 1 4 5 7 I'm trying to formulate a loop that can combine these actions: - If the line begins with a letter: replace the '\ n' after a field containing characters with a '\ t' (sed 's / \ n / \ t / g' ) - If the line... (2 Replies)
Discussion started by: dovah
2 Replies

3. Shell Programming and Scripting

awk problem - combining awk statements

i have a datafile that has several lines that look like this: 2,dataflow,Sun Mar 17 16:50:01 2013,1363539001,2990,excelsheet,660,mortar,660,4 using the following command: awk -F, '{$3=strftime("%a %b %d %T %Y,%s",$3)}1' OFS=, $DATAFILE | egrep -v "\-OLDISSUES," | ${AWK} "/${MONTH} ${DAY}... (7 Replies)
Discussion started by: SkySmart
7 Replies

4. Shell Programming and Scripting

How to combining awk commands?

I can achieve two tasks with 2 different awk commands: 1) awk -F";;WORD" '{print $2}' file | sed '/^$/d' #to find surface_word 2) awk -F"bw:|gloss:" '// {print $2}' file | sed '/\//!d; s:/*+*: + :g; s:^+::; s: *+ *$::;' #to find segmentation of surface_word Number 1) finds surface_word... (7 Replies)
Discussion started by: Viernes
7 Replies

5. Shell Programming and Scripting

combining multiple sed statements

I need to run a cronjob that will monitor a directory for files with a certain extension, when one appears I then need to run the below scripts How do I go about combining the following sed statements into one script? and also retain the original filename.? sed 's/71502FSC1206/\n&/g' # add a... (2 Replies)
Discussion started by: firefox2k2
2 Replies

6. Shell Programming and Scripting

Combining multiple rows in single row based on certain condition using awk or sed

Hi, I'm using AIX(ksh shell). > cat temp.txt "a","b",0 "c",bc",0 "a1","b1",0 "cc","cb",1 "cc","b2",1 "bb","bc",2 I want the output as: "a","b","c","bc","a1","b1" "cc","cb","cc","b2" "bb","bc" I want to combine multiple lines into single line where third column is same. Is... (1 Reply)
Discussion started by: samuelray
1 Replies

7. UNIX for Dummies Questions & Answers

Combining awk tests

It would be convenient to be able to combine awk tests. For example, suppose that I do this query: awk '$1 != "Bob" || $1 != "Linda" {print $2}' datafileIs there a reasonable way to combine the conditions into a single statement? For example, in egrep, I can do: egrep -v "Bob|Linda"... (4 Replies)
Discussion started by: treesloth
4 Replies

8. Shell Programming and Scripting

Sed: Combining Multiple Lines into one

Before I ask my actual question, is it going to be a problem that I want to run this process on a 15 Gig file that is ~140 million rows? What I'm trying to do: I have a file that looks like Color,Type,Count,Day Yellow,Full 5 Tuesday Green,Half 6 Wednesday Purple,Half 8 Tuesday ...... (3 Replies)
Discussion started by: goldfish
3 Replies

9. Shell Programming and Scripting

combining fields in awk

I am using: ps -A -o command,%cpu to get process and cpu usage figures. I want to use awk to split up the columns it returns. If I use: awk '{print "Process: "$1"\nCPU Usage: "$NF"\n"}' the $NF will get me the value in the last column, but if there is more than one word in the... (2 Replies)
Discussion started by: json4639
2 Replies

10. UNIX for Dummies Questions & Answers

combining sed commands

I would like to change the lines: originalline1 originalline2 to: originalline1new originalline1newline originalline2new originalline2newline To do this, id like to combine the commands: sed 's/^/&new/g' file > newfile1 and sed '/^/ a\\ newline\\ \\ (2 Replies)
Discussion started by: Dave724001
2 Replies
Login or Register to Ask a Question