Splitting the line in multiple lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Splitting the line in multiple lines
# 1  
Old 03-30-2008
Data Splitting the line in multiple lines

Hi Guys,

I need a help. I am pretty new to the shell level programing. I was trying to split a long lines to a multiple lines with few conditions. I goggle for the code and found some snippets and tried to modified it but I got few strange problems too.

I have to split the lines if line is longer than 120 characters, then I have to separate it by adding "\" at the end of around 120th character. Note it can only separated at delimitation positions such as space or comma. And here is the code for that

#!/bin/sh

awk '{
if ( length($0) > 120 )
{
str=$0 ;
i=0 ;

while(length(str) > 120)
{
j=0;
for (m=1; m<=120;m++)
{
letter=substr($0,i+m,1);
#printf("%s\n",letter);
if( letter !=",")
then
else
{
j=m;
# printf("%s\n",substr($0,i+1,j) );
}
fi
if( letter !=" ")
then
else
{ j=m;}
fi

}

printf("%s/\n",substr($0,i+1,j) );

i+=j ;
str=substr($0,i,length($0) );
}

if( length(str) > 1 )
printf("%s\n", substr(str,2,length(str))) ;

}
else
{
print $0
}
}' myfile.txt


But my problem was that above condition is working if put in posted order. If I used to work with equal to and put the expression after then it is not working properly.

I was also not able to use the OR expression for comma and space

I also need the program which can revert the same operation. I also read few tutorial about sed but found its quite confusing for new usersSmilieSmilie
# 2  
Old 03-30-2008
Old C programmer, huh? Welcome to high-level languages, where you don't have to reinvent strlen in every project.

man fold

perl -pe 's/(.{1,119}[ ,])/\1\\\n/g'

I don't understand the question about OR. What did you try, and how didn't it work?

Code:
if (letter == " " || letter == ",")

# 3  
Old 03-31-2008
Hi
Thanks for the reply.
Yeah I have worked on C and VB but not in these shell level programming. And main problem for me on these programing is they hardly give me error message and I can't check the condition on debug mode.

For OR logic I tried
Code:
if (letter = " " -o letter = ",")

as I found on some tutorials.
And I also read that for equals to we have to use single '=' not as '=='' like C++

Is this line do the same operation as mine???
Code:
perl -pe 's/(.{1,119}[ ,])/\1\\\n/g'

It is really difficult to find out what is happening with these code. I know few things
a) to handle the strings, we have to give ' at the start and end of the command
b) all arguments are should be separated by /
I really don't know any thing besides those from this code

SmilieSmilie
# 4  
Old 03-31-2008
You have been reading shell programming tutorials (and possibly C shell, if they advice round parentheses in the if -- that's different from Bourne shell, sort of like Java is quite unlike JavaScript, although not quite as unrelated) but the program you have written is in the awk language, which is not at all the same thing. Compare to how you can use a DLL written in another language from a C program. But with scripts, the source can be in two different languages, and often is -- you frequently put small awk or sed or Perl or TCL or ... other scripts in shell scripts where the shell's own syntax or facilities don't match the problem space well enough.

The Perl program does what I imagine your spec says it should do. Try it and see. It replaces multiples of 119 charactes or less followed by a space or a comma with the same string plus a backslash and a newline. You could do the same in sed or awk, albeit not quite as succinctly; the main concept here is a regular expression, which is a facility all of these languages provide. (I think you have it in VB, too, and there are C libraries you can link to if you like.) Read up on that, it's an extremely useful and versatile tool, quite independent of any particular language.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Multiple lines to single line

I have code as below # create temporary table `temp4277`(key(waybill_no)) select waybill_no,concat_ws('',card_type,card_series_no) cardinfo from rfid_temp_ticket where waybill_no='4277' group by... (4 Replies)
Discussion started by: kaushik02018
4 Replies

2. Shell Programming and Scripting

Splitting XML file on basis of line number into multiple file

Hi All, I have more than half million lines of XML file , wanted to split in four files in a such a way that top 7 lines should be present in each file on top and bottom line of should be present in each file at bottom. from the 8th line actual record starts and each record contains 15 lines... (14 Replies)
Discussion started by: ajju
14 Replies

3. Shell Programming and Scripting

Splitting one line into multiple lines

Hi, I have one problem scenorio as below. my source file is : cat input_file. "hi","there","how","are","you?","It","was","great","working","with","you.","hope","to","work","y ou." my output should be like. "hi","there","how","are","you?", "It","was","great","working","with",... (7 Replies)
Discussion started by: abhilash_nakka
7 Replies

4. Shell Programming and Scripting

Splitting Single line into multiple line

Hi All, I am reading a line from a file and writing it to other file. Whenever I got a particular line then I want that line to be splited into 4 line and written it to new file. e.g My line is U_ABC connector3 pin24E connector4 pin25E connector5 pin26E connector6 pin27E connector7... (2 Replies)
Discussion started by: diehard
2 Replies

5. Shell Programming and Scripting

Merge multiple lines in one line

Hi guys, So i have a input file with several sequences aligned (fasta) >NC_005930 241 bp MNMINIFIINNIFDQFIPVKLSIFSLTSVGSIIA LSWVWINTKTHWAISRSNTP-SLLLNSL WTLLITNL-NEKTNPWAPWLFSLFLLCFSFNIMSLI-PYTF-SQ TSHLSFTFGLSLPIWIMVNIAGFKNNWKKKISHLLPQGTPIYLVPVMII IETISLFIQPLTLGFRLGANLLAGHLLIFLCSCTIWE... (6 Replies)
Discussion started by: andreia
6 Replies

6. Shell Programming and Scripting

Merge multiple lines to one line when line starts with and ends with

example: comment Now_TB.table column errac is for error messages 1 - first 2 - second 3 -third ; in this example I need to be able to grab the comment as first word and ; as the last word and it might span a few lines. I need it to be put all in one line without line breaks so I can... (4 Replies)
Discussion started by: wambli
4 Replies

7. Shell Programming and Scripting

splitting a huge line of file into multiple lines with fixed number of columns

Hi, I have a huge file with a single line. But I want to break that line into lines of with each line having five columns. My file is like this: code: "hi","there","how","are","you?","It","was","great","working","with","you.","hope","to","work","you." I want it like this: code:... (1 Reply)
Discussion started by: rajsharma
1 Replies

8. Shell Programming and Scripting

Multiple lines into a single line

Hi, I've some files with the following data and i need to convert the lines between the separator ---, into a single line. I've tried with the paste cmd but my main problem is that the number of lines between the separator is not fix, it can very between 1-4 lines. Input --- 2010-02-22... (4 Replies)
Discussion started by: RickyC9999
4 Replies

9. Shell Programming and Scripting

Multiple lines into one line

Hi all I have a file with these lines good gone home What code do you need to put this on one line like this good gone home (2 Replies)
Discussion started by: hawaiifiver
2 Replies

10. Shell Programming and Scripting

Splitting a single line into multiple lines

I have a case where, I need to look into a file. Go to each line of the file, find the length of the line, if the length of the line is more than 75 chars, I need to split the line into multiple lines of 75chars max. If the length of the line is less than 75, we need not do anything. So at the... (4 Replies)
Discussion started by: thanuman
4 Replies
Login or Register to Ask a Question