Help with grep awk sed command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with grep awk sed command
# 1  
Old 04-21-2010
Help with grep awk sed command

I have a txt file with data
Code:
abc:def:ghi:jkl:mno pq stu vwx

I want to take out abc what should i do?

I try awk
Code:
 awk '/abc/ {print $1}' listfile.txt> extendedfile.txt


Last edited by pludi; 04-21-2010 at 02:33 AM.. Reason: corrected title
# 2  
Old 04-21-2010
Code:
sed 's/[^:]*\:\(.*\)$/\1/g' listfile.txt> extendedfile.txt



Cheers,
Devaraj Takhellambam

---------- Post updated at 01:18 AM ---------- Previous update was at 01:12 AM ----------

Or

Code:
awk '{print substr($0,index($0,":")+1,length)}' listfile.txt> extendedfile.txt


cheers,
Devaraj Takhellambam

Last edited by devtakh; 04-21-2010 at 02:18 AM.. Reason: code tag
# 3  
Old 04-21-2010
Quote:
Originally Posted by devtakh
Code:
sed 's/[^:]*\:\(.*\)$/\1/g' listfile.txt> extendedfile.txt

Cheers,
Devaraj Takhellambam[COLOR=#738fbf]
Sir, this is working fine, but the result i am getting in extended file is
Code:
def:ghi:jkl:mno pq stu vwx

How can I get the first field in extended file

Last edited by Learnerabc; 04-21-2010 at 02:35 AM.. Reason: wrong quote
# 4  
Old 04-21-2010
Something like this?
Code:
awk -F":" '/abc/{print $1}' listfile.txt> extendedfile.txt

# 5  
Old 04-21-2010
Thanks Everyone
# 6  
Old 04-21-2010
Awk is better for this kind of stuff, IMO, but a sed one, as you mentioned it in your subject title:

Code:
sed "s/\([^:]\):.*/\1/" file1      
abc

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed and awk giving error ./sample.sh: line 13: sed: command not found

Hi, I am running a script sample.sh in bash environment .In the script i am using sed and awk commands which when executed individually from terminal they are getting executed normally but when i give these sed and awk commands in the script it is giving the below errors :- ./sample.sh: line... (12 Replies)
Discussion started by: satishmallidi
12 Replies

2. Shell Programming and Scripting

Grep/awk/sed help

got a file as y.txt 1 abc,def,ghj 2 defj,abc.kdm,ijk 3 lmn,cbk,mno 4 tmp,tmop,abc,pkl 5 pri,chk,cbk,lmo 6 def,cbk.pro,abc.kdm i want to search in the above file the key word like abc looking for two outcomes by passing the parameter value as abc into function and the two outocmes are... (6 Replies)
Discussion started by: silgun
6 Replies

3. UNIX for Dummies Questions & Answers

grep/awk/sed?

Thread1 { x = 2 y = 10485 } Thread2 { x = 16 y = 1048 } Thread3 { x = 1 y = 1049 } Thread4 { x = 4 y = 1047 z = 500 } Suppose the above is a piece of code. I need to automate and verify that the value of x under Thread1's 2. There are several... (3 Replies)
Discussion started by: foxtron
3 Replies

4. Shell Programming and Scripting

help using sed/awk/grep

thanks for your reply. but i'm not quite sure what your code is doing. i may be using it wrong but i'm not getting what i'm supposed to get. could you please elaborate? thanks again, (6 Replies)
Discussion started by: kratos.
6 Replies

5. Shell Programming and Scripting

grep or awk or sed not sure which to use here

Hi All, I have a huge file, I need to two things from this file. I need to know the IP address or the hostname and second thing is the date&time. The file looks like this and I need to get my data from this... Trying... Connected to 204.109.172.117. Escape character is '^]'. Fri... (4 Replies)
Discussion started by: samnyc
4 Replies

6. UNIX for Dummies Questions & Answers

awk grep sed or something better

Hello all, Can anyone help with the following? :) I have file1 with 150,000 words in a list and file2 with 148,000 words in a list - all of which are in file1. I want to create a new file with the words that DO NOT match (i.e of 2000 words). I have done this very simple command , which is... (1 Reply)
Discussion started by: dr_sabz
1 Replies

7. UNIX for Dummies Questions & Answers

How could i get this by sed or grep or awk ????

------------------------------------------------------------------ Ex of Warning messgae,(Many similar lines occure for Both Test and Test1) -WARNING:Below Field not implemented in file File name: /home/test/ new/file1, msg buffer is: :Test:000948 ... (1 Reply)
Discussion started by: prsam
1 Replies

8. UNIX for Dummies Questions & Answers

Grep Sed or Awk?

I have two .txt files one called good.txt and the other one is called bad.txt. Both contain email addresses in the following format: john@john.com bob@bob.com sarah@sarah.com Basically, I want to scrub good.txt against bad.txt and save the resulting output in scrubbed.txt meaning that if... (2 Replies)
Discussion started by: holyearth
2 Replies

9. UNIX for Dummies Questions & Answers

Awk, Sed and Grep

Hello. I am an older newbie trying to learn Unix. I have a task to perform and it entails counting lines of code. Currently, I am pointing to the directory where the files are contained and performing a 'find' on the file extensions (cpp, c, html, java, etc.) and piping that info with a 'wc -l'.... (2 Replies)
Discussion started by: mastachef
2 Replies

10. Shell Programming and Scripting

Sed | Awk | Grep

Can someone help me in understanding when to use SED, AWK and GREP (3 Replies)
Discussion started by: kn.naresh
3 Replies
Login or Register to Ask a Question