Visit Our UNIX and Linux User Community


Awk/sed solution for grep,cut


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Awk/sed solution for grep,cut
# 1  
Old 07-17-2009
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

Code:
SUB: ENG123, GROUP 1
SUB: HIS124, GROUP 1
..
..

Normally , i do
Code:
grep ENG example |  cut -d ' ' -f 2 | cut -c 4-6

Is it possible to do it in simpler way using awk/sed ?


Thanks,
Priya

Last edited by zaxxon; 07-17-2009 at 01:59 AM.. Reason: code tags
# 2  
Old 07-17-2009
Next time use CODE-tags when displaying code, data or logs to enhance readability and to keep formatting like indention etc., ty.

Here are some examples in awk and sed:
Code:
$> awk -F" |," '/ENG/ {sub(/[[:alpha:]]*/,"",$2); print $2}' infile
123
$> sed -n 's/.*ENG\([[:digit:]]\+\),.*/\1/p' infile
123
$> awk '/ENG/ {print substr($2,4,3)}' infile
123

# 3  
Old 07-17-2009
Try:
Code:
awk '/ENG/ {$0=$2;FS="[A-Z,]";print $(NF-1)}' example

# 4  
Old 07-17-2009
Thanks, but this one not working

Quote:
Originally Posted by rakeshawasthi
Try:
Code:
awk '/ENG/ {$0=$2;FS="[A-Z,]";print $(NF-1)}' example


This is not giving the 123 I needed. This is giving the o/p: ENG123,

---------- Post updated at 09:13 PM ---------- Previous update was at 09:11 PM ----------

Quote:
Originally Posted by zaxxon
Next time use CODE-tags when displaying code, data or logs to enhance readability and to keep formatting like indention etc., ty.

Here are some examples in awk and sed:
Code:
$> awk -F" |," '/ENG/ {sub(/[[:alpha:]]*/,"",$2); print $2}' infile
123
$> sed -n 's/.*ENG\([[:digit:]]\+\),.*/\1/p' infile
123
$> awk '/ENG/ {print substr($2,4,3)}' infile
123


Thanks all of above are working .

Can you explain a bit about the first awk command ? I didn't understand it completely.
# 5  
Old 07-17-2009
hmm, works for me. May be depends on the version of awk.
Code:
$ cat example
SUB: ENG123, GROUP 1
SUB: HIS124, GROUP 1
$ cat ex.sh
awk '/ENG/ {$0=$2;FS="[A-Z,]";print $(NF-1)}' example
$ sh ex.sh
123
$

# 6  
Old 07-17-2009
Quote:
Originally Posted by rakeshawasthi
hmm, works for me. May be depends on the version of awk.
Code:
$ cat example
SUB: ENG123, GROUP 1
SUB: HIS124, GROUP 1
$ cat ex.sh
awk '/ENG/ {$0=$2;FS="[A-Z,]";print $(NF-1)}' example
$ sh ex.sh
123
$

Think you are right about the version.
Code:
>awk '/ENG/ {$0=$2;FS="[A-Z,]";print $(NF-1)}' ~/eg
ENG123,

Anyways , thanks for the help.
-- Priya
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #359
Difficulty: Medium
The chmod command first appeared in Version 1 AT&T UNIX.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed or awk Solution

Hi I am having a csv file like this ahsh,90.82,add,32424,ahha hhdh,98.89,hdhdh,92728,neha hshs,you,97.7,hdhdhd,are,a jsjsj,wonderful,9788,79.9,aheh ahdh,95.5,girl, 2737,jolllI need to add width="100" to the value which is greater than 90 like decimal points but less than 100 Output... (5 Replies)
Discussion started by: kshitij
5 Replies

2. UNIX for Dummies Questions & Answers

Print/cut/grep/sed/ date yyyymmdd on the filename only.

I have this filename "RBD_EXTRACT_a3468_d20131118.tar.gz" and I would like print out the "yyyymmdd" only. I use this command below, but if different command like cut or print....etc. Thanks ls RBD_EXTRACT* | sed 's/.*\(........\).tar.gz$/\1/' > test.txt (9 Replies)
Discussion started by: dotran
9 Replies

3. Shell Programming and Scripting

AWK or SED solution

Hello. I have big file data like this(part of file): .... 18210102021010000110 47401000000 021001 5166891.16 021011 5166891.16 18210602010020000110 47401000000 020701 8995421.00 021001 8995421.00 021011 8995421.00 030801 .08 18210604011020000110 47401000000 020701 9048.00 021001... (3 Replies)
Discussion started by: maxoff
3 Replies

4. 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

5. 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

6. 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

7. 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

8. Shell Programming and Scripting

cut sed grep or other?

Hi Is there a way to cut the last two characters off a word or number given that this word or number can be of varying length? I have tried something like TEST=`echo $OLD | cut -c 1-5` where $OLD is a variable containing a number like 1234567 which gives a result of 12345. This is fine... (4 Replies)
Discussion started by: rleebife
4 Replies

9. Shell Programming and Scripting

sed, grep, cut or combine?

I am a beginner at shell scripting, actually i am working on my first script right now. Anyway i have searched the world how to grep two letters from each word (it will always just be two words). For example: Example Blablabla I want my script to cut out Ex (from the first word) and Bl... (4 Replies)
Discussion started by: maskot
4 Replies

Featured Tech Videos