script to move two lines to the end of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to move two lines to the end of a file
# 1  
Old 03-06-2009
script to move two lines to the end of a file

My input file is multiline file and I am writing a script to search for a pattern and move the line with the pattern and the next line to the end of the file. Since I am trying to learn awk, I thought I would try it.

My input looks like the following:

D #testpoint 1
510.0
D #testpoint2
418.0
D "#test point 3"
0.0
D #testpoint5
0.0
D #testpoint6
-50.0

My output should look like the following:

D #testpoint 1
510.0
D #testpoint2
418.0
D #testpoint5
0.0
D #testpoint6
-50.0
D "#test point 3"
0.0

This is what I have so far:
BEGIN {
RS="^[-+]?[0-9]*\.?[0-9]+?$"
FS="\n"
}
{

getline x
nf = split(x,lines," ")
if (lines[2] ~/"#test point 3"/)
{saveme = $0
getline
saveme1 = $0
getline;}
else
print $0
next
}
Thanks for any help given.
# 2  
Old 03-06-2009
Should be something like this:

Code:
awk '
/test point 3/{
  s1=$0
  getline
  s2=$0
  next
}
{print}
END{print s1 "\n" s2}
' file

Regards
# 3  
Old 03-09-2009
Script to move two lines to the end of the file

Thanks for your answer. It works, but I need to make it a stand alone module. This is what I have done, but it comes up with the following error when I run it against the source code: syntax error The source line is 15.

I modified the code as follows:

{
if ($0 ~ /#testpoint 3/) {
s1=$0
getline
s2=$0
next}
else {
{print}
}
}
END {print s1 "\n" s2}


Thanks for your help. It is much appreciated.
# 4  
Old 03-09-2009
Quote:
Originally Posted by banjo25
Thanks for your answer. It works, but I need to make it a stand alone module. This is what I have done, but it comes up with the following error when I run it against the source code: syntax error The source line is 15.

I modified the code as follows:

{
if ($0 ~ /#testpoint 3/) {
s1=$0
getline
s2=$0
next}
else {
{print}
}
}
END {print s1 "\n" s2}


Thanks for your help. It is much appreciated.
Post the entire script within code tags.

Regards
# 5  
Old 03-10-2009
Script to move two lines to the end of the file

Hello, I resolved my syntax error. I am calling awk from within a Bourne shell script and had incorrect shell script syntax. My script now works. Thank you so much for your help.
# 6  
Old 03-10-2009
sed is also a good way to solve your problem

Code:
sed -n '/test point/ !{
	p
}
/test point/ {
	x
	n
	H
}
$ {
	x
	p
}' a.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Add strings from one file at the end of specific lines in text file

Hello All, this is my first post so I don't know if I am doing this right. I would like to append entries from a series of strings (contained in a text file) consecutively at the end of specifically labeled lines in another file. As an example: - the file that contains the values to be... (3 Replies)
Discussion started by: gus74
3 Replies

2. Shell Programming and Scripting

Put the lines from file A to end of lines in file B

I really can't figure this one out. I have 2 files, one file is a list of hostnames and the other is a list of their corresponding IPs: fileA: example.com another.org thirdie.net fileB: 1.1.1.1 2.2.2.2 3.3.3.3 I want to create a fileC that looks like: example.com 1.1.1.1... (2 Replies)
Discussion started by: zstar
2 Replies

3. Shell Programming and Scripting

Script to add backslashes to end of certain lines of text

I'd like to write up notes in a relatively readable format and then use a shell script to add LaTeX formatting. Specifically, I'm trying to figure out how to add the LaTeX newline character (\\) to the end of lines without \begin{} or \end{} statements example notes file: \begin{enumerate} --... (2 Replies)
Discussion started by: icskittles
2 Replies

4. Shell Programming and Scripting

how to move a word to the end of file

Hey guys, I want move a specific word from the middle of the text and move it the end of the file, which means I want the word to be deleted from it's line and moved to the end of file. I know how to use sed for adding a word the end of file, but I don't know how to move words. tnx (2 Replies)
Discussion started by: Johanni
2 Replies

5. Shell Programming and Scripting

deleting the lines at the end of the file.

I have a text file with two coulmn first column is just used in to show the line number, these line number are not there in the real file. I want to delete the line 16(in this file) here, even tough there is no data inside it . this empty line is causing me a problem by throwing me garbage... (12 Replies)
Discussion started by: shashi792
12 Replies

6. Shell Programming and Scripting

Move a line to end of file

Can somebody help me with a script .... Read a file /etc/inittab find the string starting with rcml and move it entirely towards the end of file. rcml:2:once:/usr/sni/aix52/rc.ml > /dev/console 2>&1 I basically want to change the startup sequence. (2 Replies)
Discussion started by: imanuk2007
2 Replies

7. Shell Programming and Scripting

Script to move the first line of a file to the end

I'm rather new to scripting, and despite my attempts at finding/writing a script to do what I need, I have not yet been successful. I have a file named "list.txt" of arbitrary length with contents in the following format: /home/user/Music/file1.mp3 /home/user/Music/file2.mp3... (21 Replies)
Discussion started by: Altay_H
21 Replies

8. Shell Programming and Scripting

Delete blank lines at the end of file

I am attempting to delete blank lines in my file and I've used this command: sed '/^$/d' $file > $file.fixed all this seems to do is copy the file and not delete the blank lines located at the end of the file. Any assistance would be greatly appreciated. (3 Replies)
Discussion started by: TL56
3 Replies

9. HP-UX

Add a column at the end of all the lines in a file

Hi Guys, :D I am very much new to UNIX. I dont have much basics of coding in UNIX, so please help me out of thi ssituation. I have a file say for ex: ABC.dtd and it contains "|" delimited data as test1|testing|test3|moving past1|runing|test4|going I need to add a column at the end... (6 Replies)
Discussion started by: ruthless
6 Replies

10. Shell Programming and Scripting

Blank Lines - End of file

Hi all I need to strip blank lines from the end of a file. I have searched and found topics on how to strip lines from the entirety of a file - however I need to limit this to only the last 3-4 lines. Any ideas? Thanks (4 Replies)
Discussion started by: saabir
4 Replies
Login or Register to Ask a Question