Split a line on positions - Shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split a line on positions - Shell
# 1  
Old 02-16-2009
Split a line on positions - Shell

Hi,
I want to split the length of a line based on the numbe of positions.

eg: ABCBEFGH IJKLMN asdfas

I want to split every 4 characters into a new line
Can i use cut or any easy way of using a single or 2 lines.

Thanks
Vijay
# 2  
Old 02-16-2009
Hammer & Screwdriver Perhaps this will work for you

Code:
> echo "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | sed -e "s/..../&~/g" | tr "~" "\n"
ABCD
EFGH
IJKL
MNOP
QRST
UVWX
YZ
> echo "ABCDEFGHIJKLMNOPQRSTUVWXYZ ABC" | sed -e "s/..../&~/g" | tr "~" "\n"
ABCD
EFGH
IJKL
MNOP
QRST
UVWX
YZ A
BC

# 3  
Old 02-16-2009
If you have a recent version of bash try:
Code:
string="ABCDEFGHIJKLM"
while [ ${#string} -gt 0 ]
do
      echo "${string:0:4}" #  4 characters 
      string="${string:4}" 
done

# 4  
Old 02-16-2009
Quote:
Originally Posted by jim mcnamara
If you have a recent version of bash try:

"recent" = any version since bash-2.0 of 1996.
Quote:
Code:
string="ABCDEFGHIJKLM"
while [ ${#string} -gt 0 ]
do
      echo "${string:0:4}" #  4 characters 
      string="${string:4}" 
done


In any POSIX shell:

Code:
string="ABCDEFGHIJKLM"
while [ ${#string} -ge 4 ]
do
    temp=${string#????}
    printf "%s\n" "${string%"$temp"}"
    string=$temp
done
[ -n "$string" ] && printf "%s\n" "$string"

# 5  
Old 02-18-2009
Split while reading a line in shell

Thanks all.

Last edited by vijaykrc; 02-18-2009 at 09:18 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split a line

I have a very long line in a file separated by "|" delimiter like below. Due to the length of the line, I find it very difficult to read to find a match line. file = temp.txt word 1| word 2 | word 3|.... I would like to read the file temp.txt and print out all words line by line like... (1 Reply)
Discussion started by: boldnbeautiful
1 Replies

2. UNIX for Dummies Questions & Answers

Split Every Line In Txt Into Separate Txt File, Named Same As The Line

Hi All Is there a way to export every line into new txt file where by the title of each txt output are same as the line ? I have this txt files containing names: Kandra Vanhooser Rhona Menefee Reynaldo Hutt Houston Rafferty Charmaine Lord Albertine Poucher Juana Maes Mitch Lobel... (2 Replies)
Discussion started by: Nexeu
2 Replies

3. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

4. Shell Programming and Scripting

sed to replace specific positions on line with file contents

Hi, I am trying to use an awk command to replace specific character positions on a line beginning with 80 with contents of another file. The line beginning with 80 in file1 is as follows: I want to replace the 000000000178800 (positions 34 - 49) on this file with the contents of... (2 Replies)
Discussion started by: nwalsh88
2 Replies

5. Shell Programming and Scripting

Split a line

I guess this has a simple solution but can't figure out now. having: x="H:a:b:c" to get H: echo $x|awk -F: {'print $1'} how can I put REST of line in another one? i.e. echo $rest a:b:c thanks ---------- Post updated at 08:58 PM ---------- Previous update was at... (5 Replies)
Discussion started by: garagonp
5 Replies

6. Shell Programming and Scripting

awk script replace positions if certain positions equal prescribed value

I am attempting to replace positions 44-46 with YYY if positions 48-50 = XXX. awk -F "" '{if (substr($0,48,3)=="XXX") $44="YYY"}1' OFS="" $filename > $tempfile But this is not working, 44-46 is still spaces in my tempfile instead of YYY. Any suggestions would be greatly appreciated. (9 Replies)
Discussion started by: halplessProblem
9 Replies

7. Shell Programming and Scripting

Split line to multiple files Awk/Sed/Shell Script help

Hi, I need help to split lines from a file into multiple files. my input look like this: 13 23 45 45 6 7 33 44 55 66 7 13 34 5 6 7 87 45 7 8 8 9 13 44 55 66 77 8 44 66 88 99 6 I want to split every 3 lines from this file to be written to individual files. (3 Replies)
Discussion started by: saint2006
3 Replies

8. Shell Programming and Scripting

split single line into two line or three lines

Dear All, I want to split single line into two line or three lines wherever “|” separated values comes using Input line test,DEMTEMPUT20100404010012,,,,,,,,|0070086|0070087, output shoule be test,DEMTEMPUT20100404010012,,,,,,,,0070086, test,DEMTEMPUT20100404010012,,,,,,,,0070087, (14 Replies)
Discussion started by: arvindng
14 Replies

9. Shell Programming and Scripting

Split a line on positions before reading complete line

Hi, I want to split before reading the complete line as the line is very big and its throwing out of memory. can you suggest. when i say #cat $inputFile | while read eachLine and use the eachLine to split its throwing out of memory as the line size is more than 10000000 characters. Can you... (1 Reply)
Discussion started by: vijaykrc
1 Replies
Login or Register to Ask a Question