Moving String in a File on the top


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Moving String in a File on the top
# 1  
Old 05-08-2012
Moving String in a File on the top

Hello,

I have a file containing data and the headers of the data and the headers of the data randomly anywhere in the file but always with the specific string col1;col2;col3

Code:
112233;1133244;112344
112233;1133244;112344
112233;1133244;112344
112233;1133244;112344
Col1;Col2;Col3
112233;1133244;112344
112233;1133244;112344
112233;1133244;112344
112233;1133244;112344

How can I move the headers i.e. Col1;col2;Col3 to the first line of the file.
# 2  
Old 05-08-2012
remove the headers line, rewrite it where you want it - one way:
Code:
awk ' /^Col/ {headers=$0; next} 
        {arr[NR]=$0; next}
        END {print headers; for(i in arr) {print arr[i]} }' inputfile > newfile

# 3  
Old 05-08-2012
This would be one way:

Code:
awk '
    /Col1;Col2;Col3/ {
        have = 1;
        print;
        for( j = 0; j < i; j++ )
            print buf[j];
        next;
    }
    !have { buf[i++] = $0; next; }
    { print; }
' input-file >output-file

# 4  
Old 05-08-2012
MySQL

Thanks Jim and Agama. Both the codes work fine.
# 5  
Old 05-09-2012
Code:
awk 'NR==1{print h} $0!=h' h='Col1;Col2;Col3' infile

Code:
awk 'f;/^[A-Z]/{printf "%s",$0 RS p; f=1} !f{p=p $0 RS}' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Need help on moving .csv file from UNIX to windows file path

Need help on moving .csv file from unix to windows file path. (1 Reply)
Discussion started by: lakshmanraok117
1 Replies

2. Shell Programming and Scripting

Moving files based on size (string to integer)

I have a log file that I want to archive out as it reaches 100MB. I am using the following to get the file size into a variable but get the error "line 5: filesize=$(wc -c < logfile.log) if then echo "is greater than 100M" else echo "is less than 100M" fi I'm sure there's something... (2 Replies)
Discussion started by: Flakman
2 Replies

3. Shell Programming and Scripting

Search for string and print top and bottom line

Hi Folks I need a one liner to parse through a log and if the string is found print the line above, the line with the string and the line below. example: The ball is green and blue Billy through the ball higer. Jane got hurt with the ball. So if I search for Billy I would need the 3... (1 Reply)
Discussion started by: bombcan
1 Replies

4. UNIX for Advanced & Expert Users

Insert string in binary file at top

How can i append a EBCDIC string of 100 bytes to 0th position of a binary file in UNIX. (4 Replies)
Discussion started by: param_it
4 Replies

5. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

6. Shell Programming and Scripting

Need help in finding filesize , moving file , gzipping file

Hi , Please help with the following questions 1) how can i find size of a file ? i have written du -k $flname > s1 . Is this right ? Any other better suggeastions ? 2) how do I use mv command for moving the file ? I need the syntax with some examples 3) Command for printing the total... (1 Reply)
Discussion started by: Learning!
1 Replies

7. AIX

Need a list of top 10 CPU using processes (also top 10 memory hogs, separately)

Okay, I am trying to come up with a multi-platform script to report top ten CPU and memory hog processes, which will be run by our enterprise monitoring application as an auto-action item when the CPU and Memory utilization gets reported as higher than a certain threshold I use top on other... (5 Replies)
Discussion started by: thenomad
5 Replies

8. AIX

Top command in AIX 4.2 (no topas, no nmon, no top)?

Is there a 'top' command equivalent in AIX 4.2 ? I already checked and I do not see the following ones anywhere: top nmon topas (1 Reply)
Discussion started by: Browser_ice
1 Replies

9. Shell Programming and Scripting

Finding & Moving Oldest File by Parsing/Sorting Date Info in File Names

I'm trying to write a script that will look in an /exports folder for the oldest export file and move it to a /staging folder. "Oldest" in this case is actually determined by date information embedded in the file names themselves. Also, the script should only move a file from /exports to... (6 Replies)
Discussion started by: nikosey
6 Replies

10. UNIX for Dummies Questions & Answers

moving a file?

i'm trying to move a file from one diectory to another and rename it at the same time. but its giving me an error . " mv: cannot unlink /dir2/file5 : No such file or directory" here is the command I'm using. mv /dir1/file1 /dir2/file5 solaris 8 (1 Reply)
Discussion started by: Holistic
1 Replies
Login or Register to Ask a Question