How to edit file to have one line entry?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to edit file to have one line entry?
# 1  
Old 03-28-2011
How to edit file to have one line entry?

Hello All,

My file content is:
Code:
DROP TABLE
     "FACT_WORLD";
 
CREATE TABLE "FACT_WORLD" (
              "AR_ID" INTEGER NOT NULL,
              "ORG_ID" INTEGER NOT NULL
              )
      DATA CAPTURE NONE
      COMPRESS YES;

I want to change this file to have entries in one line only, how to do that using sed or awk? The new file should have entries like this-
Code:
DROP TABLE "FACT_WORLD";
 
CREATE TABLE "FACT_WORLD" ("AR_ID" INTEGER NOT NULL, "ORG_ID" INTEGER NOT NULL) DATA CAPTURE NONE COMPRESS YES;

Thanks,
Akash

Last edited by pludi; 03-28-2011 at 09:11 AM..
# 2  
Old 03-28-2011
Try this:
Code:
 tr '\n' ' ' < filename | sed -e 's/;/;\n/g' -e 's/[[:blank:]][[:blank:]]*/ /g'


Last edited by Dahu; 03-28-2011 at 09:21 AM.. Reason: fixed regexp
# 3  
Old 03-29-2011
Code:
sed -n 'H;$g;s/\n//g;s/;/;\n/g;s/  */ /g;$p' file

# 4  
Old 03-29-2011
Code:
 awk 'BEGIN{RS=";"; ORS=";\n"}$1=$1'  yourSQL.sql

# 5  
Old 03-30-2011
@sk1418
I like your awk: but what does the $1=$1 mean? Set the 1st column to the 1st column?
# 6  
Old 03-30-2011
once the value of any field was reset, in this case $1 was reset by same value, awk will re-organize the whole line with default OFS=" ".

also the $1=$1 here will filter the empty line (after the last ";") out.

the example below may explain more clearly:
Code:
kent$ cat t.txt
aaa
bbb;
ccc
ddd;
eee
fff;

print the line directly:
kent$ awk 'BEGIN{RS=";";ORS=";\n"}{print $0}' t.txt
aaa
bbb;

ccc
ddd;

eee
fff;

;

$1 was reset, output lines look good, but the empty line is still there
kent$ awk 'BEGIN{RS=";";ORS=";\n"}{$1=$1;print $0}' t.txt
aaa bbb;
ccc ddd;
eee fff;
;

ok, now the empty line was filtered out with if statement
kent$ awk 'BEGIN{RS=";";ORS=";\n"}{if($1=$1) print $0}' t.txt
aaa bbb;
ccc ddd;
eee fff;

so if write it in short:
kent$ awk 'BEGIN{RS=";";ORS=";\n"}$1=$1' t.txt               
aaa bbb;
ccc ddd;
eee fff;

This User Gave Thanks to sk1418 For This Post:
# 7  
Old 03-31-2011
Thanks, that is pretty nifty...so the test
Code:
($1=$1)

will fail where no fields exist in that record, thus no print.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Edit first line of a text file

Hi friends, Issue1: I have a text file with the first line like this #chrom start end Readcount_A Normalized_Readcount_A ReadcountB Normalized_Readcount_B Fc_A_vs_B pvalue_A_vs_B FDR_A_vs_B Fc_B_vs_A pvalue_B_vs_A FDR_B_vs_A <a href="http://unix.com/">Link</a> How can I change it to the... (11 Replies)
Discussion started by: jacobs.smith
11 Replies

2. Shell Programming and Scripting

Edit a Huge one line file

We have a huge file which has just one really large line; about 500 MB. I want to 1. Count all the occurrences of a phrase 2. Replace the phrase with another. Trying to open it using vi has not helped as it complains that it is too large. Can any script help? Please advise. Thank you, (12 Replies)
Discussion started by: kaushikadya
12 Replies

3. Shell Programming and Scripting

Edit file content at the specific line.

How to edit file content at the specific line? For example at below The things to edit --> This is line 2. And it is below line 1. This is line 1. This is line 2. # i want to append some words at this row line. How? This is line 3. (8 Replies)
Discussion started by: alvin0618
8 Replies

4. Shell Programming and Scripting

Edit date entry inside a file

Hi All, I wanted to edit the date value located at /var/opt/CPsuite-R65/fw1/conf/local.scv. The date entry looks like this : :Signature (">=20100717") How can I update the date value by 1 day every other day while preserving the margins of the whole file in a shell script? I have... (5 Replies)
Discussion started by: achillesxv
5 Replies

5. Shell Programming and Scripting

shell script to edit file and delete entry

Can anyone provide me a shell script to edit a xml file and delete one entry. To do manually i can edit(vi editor) the file and 'dd' will delete the file.But I wiluld to know if I can do with a script. Thanks in advance Tannu (6 Replies)
Discussion started by: tannu
6 Replies

6. Shell Programming and Scripting

Edit entry if not equal

Ok here's the scenario, i have a ll.conf file and masterlist. #cat ll.conf 123456 banner 1234 abcdefghi #cat masterlist abcdefghi banner 123456789 what script to change ll.conf banner 123456789 from banner 1234 if they are not equal. the final output will looks like this. #cat... (4 Replies)
Discussion started by: kenshinhimura
4 Replies

7. UNIX and Linux Applications

Edit/update an /etc/group database entry (c/c++)

Hello I'm writing a program for managing accounts and groups in a linux system. My problem is how to update the members of a group in the /etc/group file,if i have to add/remove those members. total 3 variables for adding some new members to the group : char **oldmembers=grp->gr_mem; ... (1 Reply)
Discussion started by: mekos
1 Replies

8. Shell Programming and Scripting

Edit number of lines in a file to single line

Greetings, I have a file: hostnames.txt which has - # cat hostnames.txt machine1 machine2 I need the output to be saved to a variable as: HOSTNAMELIST=machine1,machine2 Please advise. Thanks, Chiru (3 Replies)
Discussion started by: chiru_h
3 Replies

9. Shell Programming and Scripting

Edit a line in a file with perl

Hi, How can I edit a line in a file? For example, a.txt contains: start: 1 2 3 4 stop: a b c d and I want to change "3" to "9" and to add "5" after "4" the result should be (a.txt): start: 1 9 3 4 5 stop: a b c d Thanks, zed (5 Replies)
Discussion started by: zed
5 Replies

10. UNIX for Dummies Questions & Answers

edit each line in the file

I am trying to edit each line in a file. The file has several columns delimitted by '|'. I need to take out the last two columns. Each line starts with a unique word through which I am storing the lines in a variable and cutting the last two colums. But, when I am echoing the line, it is... (2 Replies)
Discussion started by: chiru_h
2 Replies
Login or Register to Ask a Question