how to move the line after a certain pattern in the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to move the line after a certain pattern in the file
# 1  
Old 09-02-2008
how to move the line after a certain pattern in the file

Hi,
I have a file called /bb/bin/rstrt.
I need to move the line/entry "ccpm_load_shared_memory" after the entry
"initcorp". The problem is that there are several entries for "initcorp" in this file and I need the entry to be moved only after the first instance of "initcorp"
Is there a way to do it via sed or awk ? I am in ksh88 Thanks a lot
# 2  
Old 09-02-2008
Could you please post sample input and the desired output?
# 3  
Old 09-02-2008
The input file looks something like that:

~~~ some entries ~~~
ccpm_load_shared_memory
~~~ some entries ~~~~
initcorp
~~~ some entries ~~~
initcorp
~~~ some entries ~~~
etc....

I need to move the line "ccpm_load_shared_memory" after the first occurence of "initcorp" ....
# 4  
Old 09-02-2008
Is there only one occurrence of the pattern to be moved?

Use nawk or /usr/xpg4/bin/awk on Solaris:

Code:
awk '/ccpm_load_shared_memory/ { next }1
/initcorp/ && !_++ { print "ccpm_load_shared_memory" }
' filename


With some versions of sed (GNU Sed for example):

Code:
sed '0,/initcorp/{
/ccpm_load_shared_memory/d
s/initcorp/&\nccpm_load_shared_memory/
}' filename


Last edited by radoulov; 09-02-2008 at 12:43 PM..
# 5  
Old 09-02-2008
Thanks a lot!!!!!!!!!!!!!!!
# 6  
Old 09-03-2008
Quote:
Originally Posted by radoulov
[...]
Code:
awk '/ccpm_load_shared_memory/ { next }1
/initcorp/ && !_++ { print "ccpm_load_shared_memory" }
' filename

[...]
Oops, no need for two actions:

Code:
awk '!/ccpm_load_shared_memory/
/initcorp/ && !_++ { print "ccpm_load_shared_memory" }
' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading a file line by line and print required lines based on pattern

Hi All, i want to write a shell script read below file line by line and want to exclude the lines which contains empty value for MOUNTPOINT field. i am using centos 7 Operating system. want to read below file. # cat /tmp/d5 NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root"... (4 Replies)
Discussion started by: balu1234
4 Replies

2. Shell Programming and Scripting

Move a line to top of the file

Hi, I have a following file and it has only one occurrence of line that says "Output view:". It could be in middle somewhere ( i don't know the exact location ). I want to move it as the first line of the file. Input AAA BBBB CCCC Output view: XXXX YYYY ZZZZ Output should be: Output... (13 Replies)
Discussion started by: jakSun8
13 Replies

3. Shell Programming and Scripting

Move a text to next line in a file

Hi , I need your help for the below issue. I have a file which has data as below An error came (/u01/app/12.csv) pkg1.func1: detail s 1111-->pkg1.func1: detail s 2222--> Now pkg1.func1: .... --> can come multiple times in the second line. I need to arrange the data in the below... (9 Replies)
Discussion started by: bhaski2012
9 Replies

4. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

5. Shell Programming and Scripting

sed: how to move matched pattern to end of previous line

Hello, I'm new to this forum. I've been doing a lot of sed work lately and have found many useful tips on this forum. I've hit a roadblock in a project, though, and could really use some help. I have a text file with many lines like the following, i.e., some lines begin with a single word... (3 Replies)
Discussion started by: paroikoi
3 Replies

6. Shell Programming and Scripting

awk script to move a line after the matched pattern line

I have the following text format in a file which lists the question first and then 5 choices after that the explanantion and finally the answer. 1.The amount of time it takes for most of a worker’s occupational knowledge and skills to become obsolete has been declining because of the... (2 Replies)
Discussion started by: nanchil_guy
2 Replies

7. Shell Programming and Scripting

Searching a pattern in file and deleting th ewhole line containing the pattern

Hi All, Please can someone assist in the script I have made that searches a pattern in a file and delete the whole line containing the pattern. #!bin/sh # The pattern that user want to add to the files echo "Enter the pattern of the redirect" read value # check if the user has... (1 Reply)
Discussion started by: Shazin
1 Replies

8. 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

9. 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

10. Programming

how to move file pointer to a particular line in c

Hello experts, I ve a text file I want to go to particular line . what is the best way to do this in c ? I am tried as follows fseek ( fh, pos, SEEK_SET); but this functions moves the file pointer according to a number of bytes. Unfortunately I don't know the exact byte... (7 Replies)
Discussion started by: user_prady
7 Replies
Login or Register to Ask a Question