Break line after last "/" if length > X characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Break line after last "/" if length > X characters
# 1  
Old 10-25-2008
Break line after last "/" if length > X characters

Hello.
I am a french newbie in unix shell scripting (sorry if my english speaking is wrong).
I have a file with path and filenames in it. I want to limit the number of characters on each line and break the line if necessary. But the "break" should occur after a slash caracter "/".

Example of input file
/home/xxxx/xxxx/xxxx/xxxx/
/home/xxxx/xxxx/xxxx/xxxx/yyyy/yyyy/yyyy/

Output file (limitation to 33 characters for instance)
/home/xxxx/xxxx/xxxx/xxxx/
/home/xxxx/xxxx/xxxx/xxxx/yyyy/
yyyy/yyyy/

I already managed to do it with a awk script of about 10-12 lines with a while loop and a field separator "/" (sorry I don't have the code here). The thing works but I am sure it could have been done in a more easy and efficient way.

Have you any ideas ? Any help would appreciated.
Thanks.
# 2  
Old 10-25-2008
You can try this if you don't have any spaces in the file:

Code:
tr '/' ' ' < file | fold -s -w 33 | tr ' ' '/'

Regards
# 3  
Old 10-25-2008
Thanks for your answer Franklin52.
In fact it is a bit more complicated than that.

My exact input file looks like :
Code:
text
text
ASSIGN var1='path/xxx/xxx/xxx/xxx/filename1'
ASSIGN var2='path/yyy/yyy/yyyyyyy/yyyy/y/yyyyyy/yyyy/filename2'
ASSIGN var3='path/zzzzzz/zzzzzzzzzz/zzzzzzzzzzz/filename3'
text
text

I only want to break lines beginning with 'ASSIGN' and with more than 50 characters.

Last edited by SportBilly; 10-25-2008 at 09:06 AM..
# 4  
Old 10-25-2008
And what should be the desired output (given your sample)?
# 5  
Old 10-25-2008
The output should be :
Code:
text
text
ASSIGN var1='path/xxx/xxx/xxx/xxx/filename1'
ASSIGN var2='path/yyy/yyy/yyyyyyy/yyyy/y/yyyyyy/
yyyy/filename2'
ASSIGN var3='path/zzzzzz/zzzzzzzzzz/zzzzzzzzzzz/
filename3'
text
text

# 6  
Old 10-25-2008
Code:
sed '/^ASSIGN.\{44\}/s|.\{1,50\}/|&\
|' file

An example (often the embedded new line is not obvious):

Code:
bash-2.03$ cat file
text
text
ASSIGN var1='path/xxx/xxx/xxx/xxx/filename1'
ASSIGN var2='path/yyy/yyy/yyyyyyy/yyyy/y/yyyyyy/yyyy/filename2'
ASSIGN var3='path/zzzzzz/zzzzzzzzzz/zzzzzzzzzzz/filename3'
text
text
bash-2.03$ sed '/^ASSIGN.\{44\}/s|.\{1,50\}/|&\
|' file
text
text
ASSIGN var1='path/xxx/xxx/xxx/xxx/filename1'
ASSIGN var2='path/yyy/yyy/yyyyyyy/yyyy/y/yyyyyy/
yyyy/filename2'
ASSIGN var3='path/zzzzzz/zzzzzzzzzz/zzzzzzzzzzz/
filename3'
text
text


Last edited by radoulov; 10-25-2008 at 09:26 AM.. Reason: corrected ...
# 7  
Old 10-25-2008
Thanks for your answer.
I don't have Unix at home so I cannot test your solution.
I'll test it as soon as i get to work on Monday.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Failure: if grep "$Var" "$line" inside while read line loop

Hi everybody, I am new at Unix/Bourne shell scripting and with my youngest experiences, I will not become very old with it :o My code: #!/bin/sh set -e set -u export IFS= optl="Optl" LOCSTORCLI="/opt/lsi/storcli/storcli" ($LOCSTORCLI /c0 /vall show | grep RAID | cut -d " "... (5 Replies)
Discussion started by: Subsonic66
5 Replies

2. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies

3. UNIX for Dummies Questions & Answers

Find a string across line break (because of "segmentation fault core dumped")

Hi, thanks to a precedent post, and thanks to the reply of derekludwig of the forum, I have convert my first awk command as : test.txt is : AAAAAGHIJKLAjKMEFJKLjklABCDJkLEFGHIJKL awk -f findstring.awk test.txt > textreturn.txtfindstring.awk is : BEGIN{ SLENGTH = 3 } { ... (3 Replies)
Discussion started by: thewizarde6
3 Replies

4. Shell Programming and Scripting

Find lines with "A" then change "E" to "X" same line

I have a bunch of random character lines like ABCEDFG. I want to find all lines with "A" and then change any "E" to "X" in the same line. ALL lines with "A" will have an "X" somewhere in it. I have tried sed awk and vi editor. I get close, not quite there. I know someone has already solved this... (10 Replies)
Discussion started by: nightwatchrenba
10 Replies

5. Shell Programming and Scripting

finding the strings beween 2 characters "/" & "/" in .txt file

Hi all. I have a .txt file that I need to sort it My file is like: 1- 88 chain0 MASTER (FF-TE) FFFF 1962510 /TCK T FD2TQHVTT1 /jtagc/jtag_instreg/updateinstr_reg_1 dff1 (TI,SO) 2- ... (10 Replies)
Discussion started by: Behrouzx77
10 Replies

6. Shell Programming and Scripting

Delete characters from each line until meet character ":"

Hello, I have file that looks like this : 765327564:line1 94:line2 7865:line3 ..... 765322:linen I want to cut all the digits from the beginning of each line up to ":" character and to have everything like this : line1 line2 line3 ..... linen P.S : content of line1 ...... (8 Replies)
Discussion started by: black_fender
8 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. Shell Programming and Scripting

perl file, one line code include "length, rindex, substr", slow

Hi Everyone, # cat a.txt a;b;c;64O a;b;c;d;ee;f # cat a.pl #!/usr/bin/perl use strict; use warnings; my $tmp3 = ",,a,,b,,c,,d,,e,,f,,"; open(my $FA, "a.txt") or die "$!"; while(<$FA>) { chomp; my @tmp=split(/\;/, $_); if ( ($tmp =~ m/^(64O)/i) || ($tmp... (3 Replies)
Discussion started by: jimmy_y
3 Replies

9. Shell Programming and Scripting

How to remove "New line characters" and "spaces" at a time

Dear friends, following is the output of a script from which I want to remove spaces and new-line characters. Example:- Line1 abcdefghijklmnopqrstuvwxyz Line2 mnopqrstuvwxyzabcdefghijkl Line3 opqrstuvwxyzabcdefdefg Here in above example, at every starting line there is a “tab” &... (4 Replies)
Discussion started by: anushree.a
4 Replies

10. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question