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:
- How would I write a script which is able to take all the words before the first ':'?
- 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
try this:
awk 'BEGIN { FS=":" } { print $1 $2 $3 }' try
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
sed "s/[^:]*://" file
sed "s/:[^:]*$//;s/.*://" file
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
-
:[^:]*$ 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.
-
//; keep doing this on each line till the end of the file?
-
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.