SED scripting select


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED scripting select
# 1  
Old 05-02-2007
Error SED scripting select

Say I have a file 'example.txt' with these lines of code in it:

hello:anddasd:cheese:gerg
whatever:sdadsa:asdfasdfa:wwew
hmmmm:something:gfhfhgf:sdasdas

Question:
1. How would I write a script which is able to take all the words before the first ':'?
2. How would I write a script which is able to take all the words between the last two ':'s e.g. with "hello:anddasd:cheese:gerg" I'd be able to get the word "cheese"?

I've been looking at tutorials but they mostly focus on using replace so I'm stuck. Any ideas?

Thanks
# 2  
Old 05-02-2007
try this:

Code:
awk 'BEGIN { FS=":" } { print $1 $2 $3 }' try

# 3  
Old 05-02-2007
Sorry, I'm unfamiliar using awk and I really want to learn how to do this with sed. So how would I go about doing this in sed?

Thanks
# 4  
Old 05-02-2007
Code:
cut -d":" -f2- file

Code:
cut -d":" -f3 file

# 5  
Old 05-02-2007
Quote:
Originally Posted by i_am_a_robot
Sorry, I'm unfamiliar using awk and I really want to learn how to do this with sed. So how would I go about doing this in sed?

Thanks
Code:
sed "s/[^:]*://" file

Code:
sed "s/:[^:]*$//;s/.*://" file

# 6  
Old 05-02-2007
Jesus that was fast. I was spending hours on this yesterday trying to figure it out and you did it in a minute. So let me just go through this so I understand:

sed "s/:[^:]*$//;s/.*://" file

so 's/' puts it in select mode

1.
:[^:]*$ here's the confusing bit. So what does this mean? Here's me guessing.
Choose ':' and any character at the beginning of ':' when there are 0 or more occurrences of the preceding character before the end of the line??? I'm sure what I said is wrong but that's a guess.

2.
//; keep doing this on each line till the end of the file?

3.
s/.*:// I think I get this bit. This means any character before the next ':' right?

Help out a noob, please.
Oh, and the first line you gave me gives up to the third ':'. I just want the words before the first ':' for that bit.
# 7  
Old 05-02-2007
Quote:
so 's/' puts it in select mode

1.
:[^:]*$ here's the confusing bit. So what does this mean? Here's me guessing.
Choose ':' and any character at the beginning of ':' when there are 0 or more occurrences of the preceding character before the end of the line??? I'm sure what I said is wrong but that's a guess.

2.
//; keep doing this on each line till the end of the file?

3.
s/.*:// I think I get this bit. This means any character before the next ':' right?
Please check man SED.

Quote:
Oh, and the first line you gave me gives up to the third ':'. I just want the words before the first ':' for that bit.
Code:
sed "s/:.*//" file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Scripting - Select multiple files from numbered list

I am trying to have the user select two files from a numbered list which will eventually be turned into a variable then combined. This is probably something simple and stupid that I am doing. clear echo "Please Select the Show interface status file" select FILE1 in *; echo "Please Select the... (3 Replies)
Discussion started by: dis0wned
3 Replies

2. Shell Programming and Scripting

Sed for select and retrieve data

I would like to recover the data from 3 text tags. These three markers are located between the tags specific location <tag1> and </tag1> knowing that they are in many places. In File.txt: <tag2>txt2</tag2> <tag3>txt3</tag3> <tag4>txt4</tag4> .... <tag1> <tag2>txt2</tag2>... (3 Replies)
Discussion started by: Amad
3 Replies

3. UNIX for Dummies Questions & Answers

Turning to SED to select specific records

Hi All, I am looking for a simple concise solution most likely using sed to process the following 4 rows of data from the same record and only keeps it if the second record satisfy certain critea such as surname matches up to smith or jackson: John (firstname) Smith (surname) ... (21 Replies)
Discussion started by: gjackson123
21 Replies

4. Shell Programming and Scripting

Need help with sed scripting

Hi expert, i need URGENT help in bash shell scripting using sed: i woud like to search for the word XMLRPC which is at theend of the line below in up2date file. Once found or match serverURL=https://redhat.com/XMLRPC replace with myserver.com like this ... (1 Reply)
Discussion started by: lamoul
1 Replies

5. Shell Programming and Scripting

Sed scripting issue

Hi all! I was wondering if anyone could help suggest some solutions to an issue i'm having using the Sed command. I'm still a relative novice at this, but slowly learning and enjoying the power of batch processing. I am using Sed to extract values from a .txt file containing hundreds of... (2 Replies)
Discussion started by: StudentFitz
2 Replies

6. Shell Programming and Scripting

Shell Scripting -- sed

Hi, In one of my scripts, I am using sed to do an expression replacement. The code in the script is as under sed "s|MY_INP_Lab=""|MY_INP_Lab="${2}"|" file1, where $2=xyz_abc_mbk The EXPECTED output is in file1, all the instances ofMY_INP_Lab="" shall be replaced by... (2 Replies)
Discussion started by: vivekmattar
2 Replies

7. Shell Programming and Scripting

fuser - how to select the given PID? (awk/sed)

Hello, I'm trying to select, or well put the PID that is given by fuser to a var.. It has a wierd format and I somehow can't get it working, any awk/sed experts about? PID=`fuser $file | awk/sed....?` if ; then kill $PID fi Greetings and thanks for all your awesome help in advance! (4 Replies)
Discussion started by: TehOne
4 Replies

8. Shell Programming and Scripting

Select ip and date using sed or gawk

1.56.253.48 - - "GET " 1.6.253.48 - - "GET " 1.65.253.48 - - "GET " 1.65.253.48 - - "GET " 1.63.53.48 - - "GET " 1.65.253.48 - - "GET " 1.16.23.48 - - "GET " 1.64.25.48 - - "GET " need command which give the output 1.6.253.48 - 09/Nov/2009:07:02:24 1.65.253.48 -... (7 Replies)
Discussion started by: sagar_evc
7 Replies

9. Shell Programming and Scripting

sed scripting help need

hi all, i want to display output of sar, whichever idle time is less than 30%..i want to add HI and BYE at the starting and ending of the line. For an example: sar Linux 2.6.9-78.0.1.ELsmp (hostname) 07/10/2009 07:10:01 AM CPU %user %nice %system %iowait %idle... (7 Replies)
Discussion started by: raghur77
7 Replies

10. Shell Programming and Scripting

scripting with awk and sed

hey all, i was just wondering if it was possible to to get data from user input , and parse it through sed to remove or add what that user has entered into a flat file? do i need awk ? any help is greatly appreciated ~shan2on (2 Replies)
Discussion started by: shan2on
2 Replies
Login or Register to Ask a Question