Substring using cut/awk/sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Substring using cut/awk/sed
# 8  
Old 09-09-2010
Hi ,Thanks again... I have never used sed this way before , can u explain me the use of "...\" , ".*" & "n\" ?or direct me to a some page which will explain this usage ?
# 9  
Old 09-09-2010
This User Gave Thanks to felipe.vinturin For This Post:
# 10  
Old 09-13-2010
Hi Scrutinizer/felipe ,

I have been going through teh tutorials & they are very helpful thansk a lot!

However , I tried two slightly different statements on the same string & the o/p varied drastically , it is due to the position of "." in the statement .

e.x.1
Code:
echo "ABCDEFGHIJ20100909.txt" | sed -n -e 's/\(..\)\(...\).*\([0-9]\{8\}\).*/\1,\2,\3,&/p'

o/p : AB,CDE,20100909,ABCDEFGHIJ20100909.txt --> as desired

e.x.2
Code:
echo "ABCDEFGHIJ20100909.txt" | sed -n -e 's/\(..\)\(...\)*\([0-9]\{8\}\).*/\1,\2,\3,&/p'

o/p : ABCD,HIJ,20100909,CDEFGHIJ20100909.txt


Can anybody explain how the position of "." dot affects the result ?
thanks in advance .

Moderator's Comments:
Mod Comment Please use code tags!

Last edited by Scott; 09-13-2010 at 04:38 AM.. Reason: fuhgot teh question...
# 11  
Old 09-13-2010
The differente between the dot(.) and the asterisk(*) are the following:
Code:
. Matches any single character
(character)*match arbitrarily many occurences of (character)

Which means that the ()* matches "arbitrarily many occurences" and that's why the difference between the two results.
# 12  
Old 09-13-2010
-Post deleted-
# 13  
Old 09-13-2010
in above code its searching date patter from right to left ,but if i wana to search from left to right ?

please suggest i have tried but unable to do
# 14  
Old 09-14-2010
Like this you mean?
Code:
$ echo XXXXXXXX20100909XXXXXXXXJIHGEFDABC.txt|sed 's/.*\([0-9]\{8\}\).*\(..\).\(...\).txt$/\3,\2,\1,&/'
ABC,EF,20100909,XXXXXXXX20100909XXXXXXXXJIHGEFDABC.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using sed, awk or perl to remove substring of all lines except the first

Greetings All, I would like to find all occurences of a pattern and delete a substring from the all matching lines EXCEPT the first. For example: 1234::group:user1,user2,user3,blah1,blah2,blah3 2222::othergroup:user9,user8 4444::othergroup2:user3,blah,blah,user1 1234::group3:user5,user1 ... (11 Replies)
Discussion started by: jacksolm
11 Replies

2. Shell Programming and Scripting

Extract a substring using SED/AWK

Hi All, I have a log file in which name and version of applications are coming in the following format name It may look like following, based on the name of the application and version: XYZ OR xyz OR XyZ OR xyz I want to separate out the name and version and store them into variables.... (4 Replies)
Discussion started by: bhaskar_m
4 Replies

3. Shell Programming and Scripting

editing file with awk cut and sed

HI All, I am new to unix. I have a file would like to do some editing by using awk, cut and sed. Could anyone help? This file contain 100 lines. There are one line for example: 2,"102343454",5060,"579668","579668","579668","SIP",,,"825922","035885221283026",1,268,"00:59:00.782 APR 17... (2 Replies)
Discussion started by: mimilaw
2 Replies

4. Shell Programming and Scripting

Substring using sed or awk

I am trying to get a substring from a string stored in a variable. I tried sed with a bit help from this forum, but not successful. Here is my problem. My string is: "REPLYFILE=myfile.txt" And I need: myfile.txt (everything after the = symbol). My string is: "myfile.txt.gz.20091120.enc... (5 Replies)
Discussion started by: jamjam10k
5 Replies

5. Shell Programming and Scripting

Sed or awk cut all lines after word

Hi, sorry for newbie question :confused: can't find how to cut ? from 1000 2000 word some text1.... 100 200 300 word some text2.... 10 20 30 abc word some text3.... to some text1.... some text2.... some text3.... (7 Replies)
Discussion started by: Trump
7 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

cut in sed/awk

Hi Can i have an example where i should be able to cut columns (like for eg cut -c 1-3) in sed or awk. Regards Dhana (12 Replies)
Discussion started by: dhanamurthy
12 Replies

8. Shell Programming and Scripting

awk,sed or cut problem

Good afternoon, Sir's, I would like to seek your assistance regarding on this matter. $cat file1 111 aaaa bbb aass aaa files file1 temp temp1 pix 222 11 22 1 33 44 desired output: aaaa bbb aass files file1 temp1 222 11 22 1 33 44 thanks (7 Replies)
Discussion started by: invinzin21
7 Replies

9. Shell Programming and Scripting

sed, grep, awk, regex -- extracting a matched substring from a file/string

Ok, I'm stumped and can't seem to find relevant info. (I'm not even sure, I might have asked something similar before.): I'm trying to use shell scripting/UNIX commands to extract URLs from a fairly large web page, with a view to ultimately wrapping this in PHP with exec() and including the... (2 Replies)
Discussion started by: ropers
2 Replies

10. UNIX for Dummies Questions & Answers

cut vs. sed vs. awk ?

hi again...need new help guys:p the file contains following infos... users/abc/bla1.exe newusers/defgh/ik/albg2.exe users2/opww/ertz/qqwertzu/rwerwew.exe how to get the file content into... users/abc/ newusers/defgh/ik/ users2/opww/ertz/qqwertzu/ with... you can erase the... (5 Replies)
Discussion started by: svennie
5 Replies
Login or Register to Ask a Question