Substring using cut/awk/sed


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

Hi Gurus,I have a seemingly simple problem but struggling with it.It is as follows :
I/p string -
Code:
ABCDEFGHIJ20100909.txt

desired o/p -
Code:
AB,DEF,20100909,ABCDEFGHIJ20100909.txt

How to achieve it ?Thanks in advance.


Moderator's Comments:
Mod Comment Please use code tags, thank you

Last edited by Franklin52; 09-09-2010 at 06:36 AM..
# 2  
Old 09-09-2010
Try:
Code:
$ echo ABCDEFGHIJ20100909.txt|sed 's/\(..\).\(...\)....\([^.]*\).*/\1,\2,\3,&/'
AB,DEF,20100909,ABCDEFGHIJ20100909.txt

# 3  
Old 09-09-2010
Another way, but more specific for your case, I mean with letters and numbers:
Code:
echo "ABCDEFGHIJ20100909.txt" | sed -ne 's/\([A-Z]\{2\}\).\([A-Z]\{3\}\)[A-Z]\{4\}\([0-9]\{8\}\)\.txt$/\1,\2,\3,\0/p'

# 4  
Old 09-09-2010
Thanks a ton !

Scrutizer, can you explain how it works ?

Also , I used the following -
Code:
echo ABCDEFGHIJ20100909.txt|awk '{ print substr($0,1,2)","substr($0,3,3)","$0 }'

o/p -

Code:
AB,CDE,ABCDEFGHIJ20100909.txt

unable to get the date, I dnt want to depend on teh location of the date , I can use the code given by felipe. but I want to know.. is it possible using awk ?

Thanks !

Last edited by Scott; 09-13-2010 at 04:35 AM.. Reason: Code tags, please...
# 5  
Old 09-09-2010
Does it helps you?
Code:
echo ABCDEFGHIJ20100909.txt|awk '{ print substr($0,1,2)","substr($0,4,3)"," substr($0,11,8) "," $0}'

# 6  
Old 09-09-2010
Yes,

but since in this case I know where the date field starts , I want to make it indeppendant of teh date location.. is there anyway by which I can check the occurance of date & print it ?
# 7  
Old 09-09-2010
Code:
$ echo ABCDEFGHIJXXXXXXXXXXXXX20100909XXXXXXXX.txt|sed 's/\(..\).\(...\).*\([0-9]\{8\}\).*/\1,\2,\3,&/'
AB,DEF,20100909,ABCDEFGHIJXXXXXXXXXXXXX20100909XXXXXXXX.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