Substring using cut/awk/sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Substring using cut/awk/sed
# 15  
Old 09-24-2010
Hi ,

I have been trying various combinations of . & * but I have not yet completely understood how the placement of "." affects the o/p & can't use the [tag] sed [/tag] for this purpose very convincingly. following are some examples :

Code:
echo MPMTR20100706043000.txt|sed -n -e 's/\(..\)\(...\)\([0-9]\{8\}\)/\2,\1,\3,&/p'
MTR,MP,20100706,MPMTR20100706043000.txt

echo MPMTR20100701043000.txt|sed -n -e 's/\(..\)\(...\)\([0-9]\{8\}\)/\1,\2,\3,&/p'
MP,MTR,20100701,MPMTR20100701043000.txt

echo MPMTR20100706043000.txt|sed -n -e 's/\(..\)\(..\)\([0-9]\{8\}\)/\1,\2,\3,&/p'
MPM,TR,20100706,PMTR20100706043000.txt

echo MPMTR20100706043000.txt|sed -n -e 's/\(..\)\(.\)\([0-9]\{8\}\)/\1,\2,\3,&/p'
MPMT,R,20100706,MTR20100706043000.txt

echo MPMTR20100706043000.txt|sed -n -e 's/\(.\)\(..\)\([0-9]\{8\}\)/\1,\2,\3,&/p'
MPM,TR,20100706,MTR20100706043000.txt

echo MPMTR20100706043000.txt|sed -n -e 's/\(\)\(.\)\([0-9]\{8\}\)/\1,\2,\3,&/p'
MPMT,R,20100706,R20100706043000.txt

echo MPMTR20100706043000.txt|sed -n -e 's/\(\)\(\)\([0-9]\{8\}\)/\1,\2,\3,&/p'
MPMTR,,20100706,20100706043000.txt

can anyone please explain why the filename is getting clipped from left & why first field is not of 2 characters ?

Thanks!
# 16  
Old 09-24-2010
Sorry, but I didn't understand your problem! In which of the statements above is your problem?
# 17  
Old 09-27-2010
sorry for the late response .

Please ignore the first two o/p s .

1. In the 3rd o/p , even though I have selected first two matching occurrances it is selecting 3 characters

2. If I reduce one more dot for second string it selects 4 character for 1st field & equal number of characters get clipped from the filename in o/p.

can you explain how this happens ?

Thanks ,
sumoka.
# 18  
Old 10-20-2010
you can use cut,
Code:
main=`echo "ABCDEFGHIJ20100909.txt"`
first=`echo $main | cut -c1-2`
second=`echo $main | cut -c3-5`
third=`echo $main | cut -c11-18`
echo "$first,$second,$third,$main"

Best Luck.
# 19  
Old 10-20-2010
Quote:
Originally Posted by sumoka
sorry for the late response .

Please ignore the first two o/p s .

1. In the 3rd o/p , even though I have selected first two matching occurrances it is selecting 3 characters

2. If I reduce one more dot for second string it selects 4 character for 1st field & equal number of characters get clipped from the filename in o/p.

can you explain how this happens ?

Thanks ,
sumoka.
In the third example this is because the first character is not part of the search and replace operation (and likewise aren't the last 10 characters). Look what happens when we do this:
Code:
$ echo MPMTR20100706043000.txt|sed -n -e 's/\(..\)\(..\)\([0-9]\{8\}\)//p'
M043000.txt

Code:
$ echo MPMTR20100706043000.txt|sed -n -e 's/\(..\)\(..\)\([0-9]\{8\}\).*//p'
M

Code:
$ echo MPMTR20100706043000.txt|sed -n -e 's/.\(..\)\(..\)\([0-9]\{8\}\).*//p'

Code:
$ echo MPMTR20100706043000.txt|sed -n -e 's/.\(..\)\(..\)\([0-9]\{8\}\).*/\1,\2,\3,&/p'
PM,TR,20100706,MPMTR20100706043000.txt

# 20  
Old 10-20-2010
Quote:
Originally Posted by sumoka
sorry for the late response .

Please ignore the first two o/p s .

1. In the 3rd o/p , even though I have selected first two matching occurrances it is selecting 3 characters

2. If I reduce one more dot for second string it selects 4 character for 1st field & equal number of characters get clipped from the filename in o/p.

can you explain how this happens ?

Thanks ,
sumoka.
Because your coding has contains some mistakes..

Code:
echo MPMTR20100706043000.txt|sed -n -e 's/\(..\)\(..\)\([0-9]\{8\}\)/\1,\2,\3,&/p'
MPM,TR,20100706,PMTR20100706043000.txt

your string is "two chars + two chars + 8 numbers"
but after the second "two chars" there is a char before the 8 numbers

so your code is must be is below

Code:
# echo MPMTR20100706043000.txt|sed -n -e 's/\(..\)\(..\).\([0-9]\{8\}\)/\1,\2,\3,&/p'
MP,MT,20100706,MPMTR20100706043000.txt

# 21  
Old 10-20-2010
sed will try to match the biggest pattern possible

Code:
# echo MPMTR20100706043000.txt|sed -n -e 's/\([0-9][0-9]\).*\([0-9][0-9]\)/\1,\2,&/p'
MPMTR20,00,20100706043000.txt
# echo MPMTR20100706043000.txt|sed -n -e 's/\([0-9][0-9]\).*\([0-9][0-9]\)/\1,\2,&/gp'
MPMTR20,00,20100706043000.txt
# echo MPMTR20100706043000.txt|sed -n -e 's/\([0-9][0-9]\).*\([0-9][0-9]\)/\1,\2,&/1p'
MPMTR20,00,20100706043000.txt
#

and the & will be what is matched between the / / but it looks like a right expansion is performed since the .txt still remains

Note : The following will match nothing whereas we could have thought of a reverse search for the pattern like :
20100706043000
2010070604300
201007060430
20100706043
2010070604
...
see :
Code:
# echo MPMTR20100706043000.txt|sed -n -e 's/\([0-9][0-9]\).*\([0-9][0-9]\)/\1,\2,&/2p'
# echo MPMTR20100706043000.txt|sed -n -e 's/\([0-9][0-9]\).*\([0-9][0-9]\)/\1,\2,&/3p'
#

Note : that the \1 and \2 have their left and right expansion automatically performed even if not explicitly specified in the \( ...\) matching : look
Code:
# echo MPMTR20100706043000.txt|sed -n -e 's/\([0-9][0-9]\).*\(3[0-9][0-9]\)/\1,\2,&/p'
MPMTR20,300,20100706043000.txt


Last edited by ctsgnb; 10-20-2010 at 05:45 AM..
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