Word wrap with sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Word wrap with sed
# 1  
Old 11-19-2009
Word wrap with sed

Hi,

I got some timetable in a file but it is all mixed up like this

01:00 hgrtwhrt #104:00 tyergethr05:00
tqqrthd qrth #107:00 qhtrhqerth10:00 qerthrthqr qtrqthr
qthrrt11:00 thqrthqrthrr
rthgreth #212:00 trhrthrth14:00 wrthwrtwrqrthwrthwr
#2116:00 trqhthtr: rthrthr17:00 rtwhtrhwrth rthwrt ghrthrw #119:00
rwhrt rwthwrtr #322:00 wrthrwth wrhrths23:00 wtrhwr
twrthrthwrt #1

Is it possible to insert a word wrap?
I'd like to get a list like this

01:00 hgrtwhrt #1
04:00 tyergethr
05:00 tqqrthd qrth #1
07:00 qhtrhqerth
10:00 qerthrthqr qtrqthr

I thought about a word wrap 2 characters before each colon?
Thanks for your help
# 2  
Old 11-19-2009
Code:
echo $(cat file1) | sed -e "s/\([0-9][0-9]:\)/@\1/g" | awk -v RS="@" '1'

01:00 hgrtwhrt #1
04:00 tyergethr
05:00 tqqrthd qrth #1
07:00 qhtrhqerth
10:00 qerthrthqr qtrqthr qthrrt
11:00 thqrthqrthrr rthgreth #2
12:00 trhrthrth
14:00 wrthwrtwrqrthwrthwr #21
16:00 trqhthtr: rthrthr
17:00 rtwhtrhwrth rthwrt ghrthrw #1
19:00 rwhrt rwthwrtr #3
22:00 wrthrwth wrhrths
23:00 wtrhwr twrthrthwrt #1

Umm. Will try to simplify this Smilie
# 3  
Old 11-19-2009
Try the following awk command :
Code:
awk '{gsub(/..:/, "\n&"); print}' inputfile

Jean-Pierre.
# 4  
Old 11-19-2009
Thanks to the both of you
# 5  
Old 11-19-2009
My previous solution doesn't manage the splitted record.
A new solution :
Code:
awk -v RS="" '{ gsub(/\n/, " ");gsub(/[0-9][0-9]:/, "\n&");print}' file1.dat

Just a little problem, there is an empty record generated at the begining of the result.


Jean-Pierre.
# 6  
Old 11-19-2009
the easiest way is just to use sed only without awk as below (code will be more fast):-
kindly note that there is a new line in the two line code below so kindly write it as it is literally:


Code:
cat inpu_file | xargs | sed -e 's/\([0-9][0-9]:\)/\
\1/g' 



output:-

Code:
01:00 hgrtwhrt #1
04:00 tyergethr
05:00 tqqrthd qrth #1
07:00 qhtrhqerth
10:00 qerthrthqr qtrqthr qthrrt
11:00 thqrthqrthrr rthgreth #2
12:00 trhrthrth
14:00 wrthwrtwrqrthwrthwr #21
16:00 trqhthtr: rthrthr
17:00 rtwhtrhwrth rthwrt ghrthrw #1
19:00 rwhrt rwthwrtr #3
22:00 wrthrwth wrhrths
23:00 wtrhwr twrthrthwrt #1

SmilieSmilieSmilieSmilieSmilieSmilieSmilieSmilie

Last edited by ahmad.diab; 11-19-2009 at 01:14 PM..
# 7  
Old 11-22-2009
change the whole file into one line.

Code:
echo $(cat file1)
cat inpu_file | xargs
xargs < urfile
awk -v RS="" 'gsub(/\n/, " ")1'  
tr -d '\n' < urfile
awk '{printf $0}' urfile


Last edited by rdcwayx; 11-24-2009 at 01:24 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Uuencode a html file with word wrap

Hi, I have a file myfile.txt as seen below. Name Age Days --------- ------- -------- ABC 5 23 DEF 20 120 When i cat this file into a html file and send mail, I could see the file is opened as seen below. Name Age Days --------- ... (7 Replies)
Discussion started by: jayadanabalan
7 Replies

2. UNIX for Dummies Questions & Answers

Word Wrap .CSV Fils

Is there a generic method for applying word wrap to all cells for a .csv file? (6 Replies)
Discussion started by: jimmyf
6 Replies

3. Shell Programming and Scripting

Replacing a particular word with another word in all the xml's under a particular directory with sed

Hi Folks, Could you please advise what will be the SED command to replace a word in all xml's under a particular directory for example let say I rite now at the following below location $ cd /ter/rap/config now under config directory there will be lots of xml file , now my objective is to... (1 Reply)
Discussion started by: punpun66
1 Replies

4. UNIX for Dummies Questions & Answers

word wrap does not work

I have an input file like this, 79 #--------------------------------------------------------------------------- 80 #- define generic contact 81 cat > ${NAGIOS_ETC}/contact-generic.cfg <<-'EOF' 82 define contact { 83 name ... (4 Replies)
Discussion started by: ramky79
4 Replies

5. UNIX for Dummies Questions & Answers

Word Wrap

When I cat a file that has several hundred characters in a line, the right hand side is truncated. How can I make everything displayed on my screen word wrap? (6 Replies)
Discussion started by: bsimon
6 Replies

6. UNIX for Dummies Questions & Answers

line wrap in sed

I tried line wrap in sed by sed -l 20 ' n' and as far as i understand each long line would be split into lenght of 20 characters in each. but thats not happening.wats going wrong:confused: (5 Replies)
Discussion started by: mithun1!
5 Replies

7. UNIX for Dummies Questions & Answers

How to print line starts with specific word and contains specific word using sed?

Hi, I have gone through may posts and dint find exact solution for my requirement. I have file which consists below data and same file have lot of other data. <MAPPING DESCRIPTION ='' ISVALID ='YES' NAME='m_TASK_UPDATE' OBJECTVERSION ='1'> <MAPPING DESCRIPTION ='' ISVALID ='NO'... (11 Replies)
Discussion started by: tmalik79
11 Replies

8. HP-UX

word wrap issue with grep

in my HP-Unix environment I continue to have issues seeing the whole file path when I do a grep Example: >ps -ef |grep test > /testpath/is/here/ should see: >ps -ef |grep test > /testpath/is/here/not/here/test Is there a setting to turn word wrap on/off? It works fine in our AIX... (4 Replies)
Discussion started by: bowtiextreme
4 Replies

9. Shell Programming and Scripting

Should I use sed/ grep/awk for wrap file?

Hi, This is my first time post a new thread. I have been trying to work on this for the past 2 days and could not find any good solution. I have 1 long long line ( EDI wrapped file) like below: NEW*SR*04411763447*279*278*Q~*ZR*AAV*SR*04511763460*SQ*21B37F04~HL*305*304*Q~K~SN1*1*1*SR*05511763461*... (6 Replies)
Discussion started by: vanda_25
6 Replies

10. UNIX for Dummies Questions & Answers

word wrap in vi

I am using vi to edit shell scripts, but whenever I get to the end of the line it goes to the next line, and when I run the script it considers whatever was placed onthe next line a new command...I guess this has to do with word wrap- how do I continue to write on one line? (3 Replies)
Discussion started by: dangral
3 Replies
Login or Register to Ask a Question