Shell Script to convert multilines to single lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script to convert multilines to single lines
# 1  
Old 03-31-2010
Shell Script to convert multilines to single lines

Hi,

I need to create an script which reads multi lines and convert it to single line or enter escape sequence at end "\".
e.g.

Code:
#!/bin/sh

echo -e "provide me query to run"
read query 

create table test(
id int(11),
name char);

echo $query

But it's failing because of multi line i need output as follows:

Code:
create table test(id int(11),name char);

For this i tried

Code:
read query '+'  \

but it fails. Is there any way to do it means read parse multi lines into single line ?

Thank you.

Last edited by Franklin52; 03-31-2010 at 08:08 AM.. Reason: Please use code tags!
# 2  
Old 03-31-2010
Not sure if this is what you want but you can try this:
Code:
awk '/create/{c=2}c-->0{printf $0;next}1' file

# 3  
Old 03-31-2010
Code:
nawk '1' ORS="" infile.txt

input:-
Code:
create table test(
id int(11),
name char);

output:-

Code:
create table test(id int(11),name char);

SmilieSmilieSmilie
# 4  
Old 03-31-2010
Quote:
Originally Posted by ahmad.diab
Code:
nawk '1' ORS="" infile.txt

input:-
Code:
create table test(
id int(11),
name char);

output:-

Code:
create table test(id int(11),name char);

SmilieSmilieSmilie
And how about the other lines of the script?
He only needs to convert the given three lines of that script to one line.
# 5  
Old 03-31-2010
Dear Franklin52;
he said in his thread:-

Quote:
I need to create an script which reads multi lines and convert it to single line or enter escape sequence at end "\".
I think there are no other lines than the ones that he will put using read function.

another question ...what if the the multi lines (begin with "create") contain more than 3 row? then your code will fail as well.

Regards

Last edited by ahmad.diab; 03-31-2010 at 10:43 AM..
# 6  
Old 03-31-2010
Try this:

Code:
awk '{print $0}' infile.txt | tr -d "\n"


Last edited by radoulov; 03-31-2010 at 11:29 AM.. Reason: Please use code tags!
# 7  
Old 03-31-2010
Quote:
Originally Posted by ahmad.diab
Dear Franklin52;
another question ...what if the the multi lines (begin with "create") contain more than 3 row? then your code will fail as well.

Regards
You're right, but the OP didn't mention anything like that so I assume that there is only one SQL command in the script otherwise the OP should clarify his question.

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Merge multi-lines into one single line using shell script or Linux command

Hi, Can anyone help me for merge the following multi-line log which beginning with a " and line ending with ": into one line. *****Original Log***** 087;2008-12-06;084403;"mc;;SYHLR6AP1D\LNZW;AD-703;1;12475;SYHLR6AP1B;1.1.1.1;0000000062;HGPDI:MSISDN=12345678,APNID=1,EQOSID=365;... (3 Replies)
Discussion started by: rajeshlinux2010
3 Replies

2. Shell Programming and Scripting

Shell script to convert IP range csv into a list of single IP's

Hi All, I am looking for some help to convert a csv with IP ranges in.. in the format e.g. 1.1.1.2, 1.1.1.5 2.1.1.10, 2.1.1.20 and would be looking to output as follows: 1.1.1.2 1.1.1.3 1.1.1.4 1.1.1.5 2.1.1.10 2.1.1.11 etc etc up to 2.1.1.20 I have tried a few google... (4 Replies)
Discussion started by: zippyzip
4 Replies

3. Shell Programming and Scripting

Convert Singleline to Multilines

Hi All, Could you please help to get the below output: Input: J1,ELLA_1,ISDR,JJK,TRS J2,ROSTER,JACK J3,HUP ... ... ... Output: J1,ELLA_1 J1,ISDR J1,JJK J1,TRS (3 Replies)
Discussion started by: unme
3 Replies

4. Shell Programming and Scripting

convert to shell script

how to convert these code to shell script #include<stdio.h> #include<conio.h> main() { int i,j,a=0,b=0,c=0,f,t,al,ta; int a1, max, n, n1,p,k=0; printf(“\n enter no.of resources”); scanf(“%d”,n1); printf(“\nenter the max no .of resources for each type”); for(i=0;i<n1;i++)... (4 Replies)
Discussion started by: syah
4 Replies

5. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

6. Shell Programming and Scripting

how to convert a shell script to a php script for displaying next word after pattern match

I have a shell script which I made with the help of this forum #!/bin/sh RuleNum=$1 cat bw_rules | sed 's/^.*-x //' | awk -v var=$RuleNum '$1==var {for(i=1;i<=NF;i++) {if($i=="-bwout") print $(i+3),$(i+1)}}' Basically I have a pages after pages of bandwidth rules and the script gives... (0 Replies)
Discussion started by: sb245
0 Replies

7. Shell Programming and Scripting

Run a single script in different shell

Hi All, I am new in UNIX.I have a situation here.:confused: I have a script which have to be run in every shell (like KSH,BASH,CSH). Script may be any thing. But i don't know how to do it.Because the syntax of every shell may be different. Please advise.. Thanks for your kind advise in... (5 Replies)
Discussion started by: jdash.ps
5 Replies

8. Shell Programming and Scripting

here-doc convert 2 script convert to single script?

I have my main script calling another script to retrive a "ls -alt" of a directory that's located in a remote location I'm sftping into. main.sh #!/bin/ksh getLS.sh > output.txt getLS.sh #!/bin/sh /home<..>/sftp <host@ip> <<! cd /some/dir/Log ls -alt quit ! Basically I'd like to be... (2 Replies)
Discussion started by: yongho
2 Replies

9. Shell Programming and Scripting

Single Instance of a Shell Script

Hi, I have a shell script. What should I do to allow only single instance of the script to be run by a user at a time. That is, Only one user can run that script at a given point of time. Please help.. Its very important for my project Thanks in advance (4 Replies)
Discussion started by: pathanjalireddy
4 Replies

10. Shell Programming and Scripting

single input shell script?

hey, i'm trying to write a shell script which accepts: operand operator operand then, the script would see which operator it is (using case) and calculate it... but i dont know how to do it correctly with $1 $2 $3... (eliminating accepting separate inputs) (1 Reply)
Discussion started by: quipy
1 Replies
Login or Register to Ask a Question