[grep awk cut] > awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [grep awk cut] > awk
# 1  
Old 08-02-2007
Bug [grep awk cut] > awk

Hi, I'm very new to scripting.


grep $s $filename | awk '{print $2}' | cut -c 1-8


How can I optimize this using a single awk?

I tried:
awk '/$s/ {print $2}' $filename | cut -c 1-8

However didn't work, I think the awk is not recognizing $s and the verbal is something else.
# 2  
Old 08-02-2007
Can you tell us about your requirement.. What exactly are you trying to do.

Are you looking for something like this
Code:
grep $s $filename| awk '{print substr($2,1,8)}'

# 3  
Old 08-02-2007
May be,
Code:
awk -v var=$s '/$0 ~ var/ {print substr($2,1,8)}' $filename


Last edited by ranj@chn; 08-02-2007 at 02:47 AM.. Reason: typo
# 4  
Old 08-02-2007
Quote:
Originally Posted by lorcan
Can you tell us about your requirement.. What exactly are you trying to do.

Are you looking for something like this
Code:
grep $s $filename| awk '{print substr($2,1,8)}'


further in a single awk

var=some_pattern

( the value of s is assigned here )

Code:
awk -v var=$a '/'$a'/ { print substr($2, 1, 8) }' filename

If not this,

post the sample input and sample output Smilie
# 5  
Old 08-02-2007
Quote:
Originally Posted by firdousamir
Hi, I'm very new to scripting.


grep $s $filename | awk '{print $2}' | cut -c 1-8


How can I optimize this using a single awk?

I tried:
awk '/$s/ {print $2}' $filename | cut -c 1-8

However didn't work, I think the awk is not recognizing $s and the verbal is something else.
Not exactly an awk solution. I believe this should work

Code:
sed -n -e "/$s/s/^[^ ]* \(.\{8\}\).*/\1/p" $filename

# 6  
Old 08-02-2007
Try...
Code:
awk '$0~s {print substr($2,1,8)}' s=$s $filename

# 7  
Old 08-02-2007
Thanks all.

I got the stuff worked. This was my first posting in unix.com. I just loved you people.

Thanks again.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using a combination of sort/cut/grep/awk/join/paste/sed

I have a file and need to only select users that have a shell of “/bin/bash” in the line using awk or sed please help (4 Replies)
Discussion started by: boyboy1212
4 Replies

2. Shell Programming and Scripting

GREP/CUT/AWK to Arrays

hi people, I have a text file containing data, seperated by TAB. I want to process this tab'ed data as variable. how can I assign this? Ex: Code: 11aaa 12000 13aaa 14aaa 15aaa 16aaa 17aaa 21aaa 22000 23aaa 24aaa 25aaa 26aaa 27aaa 31aaa 32000 33aaa 34aaa 35aaa 36aaa 37aaa... (1 Reply)
Discussion started by: gc_sw
1 Replies

3. Shell Programming and Scripting

Read content between xml tags with awk, grep, awk or what ever...

Hello, I trying to extract text that is surrounded by xml-tags. I tried this cat tst.xml | egrep "<SERVER>.*</SERVER>" |sed -e "s/<SERVER>\(.*\)<\/SERVER>/\1/"|tr "|" " " which works perfect, if the start-tag and the end-tag are in the same line, e.g.: <tag1>Hello Linux-Users</tag1> ... (5 Replies)
Discussion started by: Sebi0815
5 Replies

4. Shell Programming and Scripting

Using grep and cut within awk

Hi My input file looks like as follows: say a.txt "aaaa cc","224 AW","ss cccccc","P06 09/10","dddddd" "aaaa cc","224 AW","ss cccccc","P06 09/10","dddddd" "aaaa cc","224 AW","ss cccccc","P06 09/10","dddddd" "aaaa cc","224 AW","ss cccccc","P06 09/10","dddddd" "aaaa cc","224 AW","ss... (5 Replies)
Discussion started by: bittoo
5 Replies

5. UNIX for Dummies Questions & Answers

Awk/sed solution for grep,cut

Hi, From the file "example" with lines like below, I need the int value associated with ENG , i.e, 123 SUB: ENG123, GROUP 1 SUB: HIS124, GROUP 1 .. .. Normally , i do grep ENG example | cut -d ' ' -f 2 | cut -c 4-6 Is it possible to do it in simpler way using awk/sed ? ... (5 Replies)
Discussion started by: priyam
5 Replies

6. Shell Programming and Scripting

Sed Awk Cut Grep Combination Help ?

I have been reading for a few hours trying to educate myself enough to accomplish this task, so please know I have performed some research. Unfortunately, I am not a *NIX scripting expert, or a coder. I come from a network background instead. SO, here is my desired outcome. I have some Cisco... (5 Replies)
Discussion started by: abbzer0
5 Replies

7. Shell Programming and Scripting

Is it better to grep and pipe to awk, or to seach with awk itself

This may just be a lack of experience talking, but I always assumed that when possible it was better to use a commands built in abilities rather than to pipe to a bunch of commands. I wrote a (very simple) script a while back that was meant to pull out a certain error code, and report back what... (4 Replies)
Discussion started by: DeCoTwc
4 Replies

8. Shell Programming and Scripting

Want to use awk instead of cut

I want to use awk instead of cut command. Following is my code: line="slNumber=US78AJF11643, slName=USJFKAAUSYDAAUL80441032900095, dummy sl found? sqlca.sqlcode=0" sl_WORD=`echo $line| cut -f 1 -d','` sl=`echo $sl_WORD | cut -f 2 -d'='` echo "$sl" Please suggest me about the code. ... (5 Replies)
Discussion started by: rinku
5 Replies

9. Shell Programming and Scripting

HELP! using cut/awk

how would i write a shell script to show the number of lines in which int variable appears in a c++ program. how would i do this using cut or awk methods is it possbile and having a output similar to this x, number of apperances = y, number of apperances = (2 Replies)
Discussion started by: deadleg
2 Replies
Login or Register to Ask a Question