Move a text to next line in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Move a text to next line in a file
# 8  
Old 05-14-2013
Quote:
Originally Posted by Yoda
Code:
awk '
        /pkg1\.func1/ {                 # Check if pattern: pkg1.func1 present in current record.
                gsub ("-->", RS)        # Global Substitute "-->" with RS (RS is a special awk variable, by default a newline)
        }
        1                               # 1 == true (if true, the default awk action is to print current record)
' file


Many thanks for this explanation.
It is totally clear now.
# 9  
Old 05-22-2013
Quote:
Originally Posted by Yoda
Code:
awk '
        /pkg1\.func1/ {                 # Check if pattern: pkg1.func1 present in current record.
                gsub ("-->", RS)        # Global Substitute "-->" with RS (RS is a special awk variable, by default a newline)
        }
        1                               # 1 == true (if true, the default awk action is to print current record)
' file


Hi,

I want to do some added functionality in this file.
Now my file will ahve this type of text.

Quote:
An error came (/u01/app/12.csv)
pkg1.func1: 2.detail is loc error-->pkg1.func1: 1.detail is dept error-->
1. and 2. are the number s which will come along with previous message.This number cab be of n digits.

Now I need to arrange the text in teh below way.

Quote:
An error came (/u01/app/12.csv)
pkg1.func1: detail is dept error
pkg1.func1: detail is loc error
Now I have arranged the text as per the numeric value associated with text and replaced the numeric value in final text.

Thanks in advance.
# 10  
Old 05-22-2013
Starting with Yoda's proposal, you may try:
Code:
$ awk     '/pkg1\.func1/  {n = split ($0, TMP, "-->")
                         for (i=1; i<n; i++)    {m = match (TMP[i], /: [0-9]*\./)
                                                 x = substr (TMP[i], RSTART+2, RLENGTH-3) + 0
                                                 sub (/: [0-9]*\./, ": ", TMP[i])
                                                 OutArr[x] = TMP[i]
                                                 if (x > max) max = x
                                                }
                         for (i=1; i<=max; i++)  if (OutArr[i]) print OutArr[i]   
                         delete OutArr
                         max = 0
                         next
                        }
         1
        ' file
An error came (/u01/app/12.csv)
pkg1.func1: detail is dept error
pkg1.func1: detail is loc error


Last edited by RudiC; 05-22-2013 at 08:07 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

3. Shell Programming and Scripting

move string in a text file

Hi all! I need some help in changing a text file. I have the following text file: 1,050406259214736,00010,5000,MZM,V050,20131231,EMIG 2,Available,20061126T10:40:15,vs,, 2,Used,20110813T12:32:35,,825351585,1411012901443027 1,050410256649750,00010,5000,MZM,V050,20131231,EMIG ... (2 Replies)
Discussion started by: fretagi
2 Replies

4. Shell Programming and Scripting

awk - if field is empty, move line to new file

I have a script with this statement: /usr/xpg4/bin/awk -F"" 'NR==FNR{s=$2;next}{printf "%s\"%s\"\n", $0, s}' LOOKUP.TXT finallistnew.txt >test.txt I want to include logic or an additional step that says if there is no data in field 3, move the whole line out of test.txt into an additional... (9 Replies)
Discussion started by: scriptr2be
9 Replies

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

6. UNIX for Dummies Questions & Answers

Using grep to move files that contain a line of text

I have a folder with about 4000 files in it. I need to extract the files that contain a certain line of text to another directory. for example files with 'ns1.biz.rr.com' need to be extracted to the directory above or some other directory. I tried using the following but was not successful. (6 Replies)
Discussion started by: spartan22
6 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

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... (5 Replies)
Discussion started by: aoussenko
5 Replies

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

10. Shell Programming and Scripting

script to move text in file?

ok i asked around to a few ppl and they said to use sed or awk to do what i want.. but i cant figure out how to use it like that.. anyway i have a text file that is 10k lines long.. i need to move text from the end of a line after the ? and move it to the front of the line then add a | after it.... (3 Replies)
Discussion started by: wckdkl0wn
3 Replies
Login or Register to Ask a Question