Appending the last few columns to the front


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Appending the last few columns to the front
# 1  
Old 07-08-2008
Power Appending the last few columns to the front

Hi

consider this as the first line
00010015 MORGAN STANLEY & CO INCORPORATED N 110 INVESTAR 1 0001OT NJ 201-830-5055 01-Jan-1974 00:00:00 1 01-May-2008 00:00:00 05-Jun-2008 13:34:18 0001 - From SMSRun1_GIDQA02
Consider this as the second line
00010015 MORGAN STANLEY & CO INCORPORATED N 110 INVESTAR 1 0001OT NJ 201-830-5055 01-Jan-1974 00:00:00 1 01-May-2008 00:00:00 05-Jun-2008 13:34:18 0001 egesdegjeog rogjeogje - From SMSRun1_GIDQA02

Here i need to cut "- From SMSRun1_GIDQA02" from the end of each line and need to append it at the front as "From SMSRun1_GIDQA02 - ". So that the output looks like

From SMSRun1_GIDQA02 - 00010015 MORGAN STANLEY & CO INCORPORATED N 110 INVESTAR 1 0001OT NJ 201-830-5055 01-Jan-1974 00:00:00 1 01-May-2008 00:00:00 05-Jun-2008 13:34:18 0001

But the problem here is that the position of "- From SMSRun1_GIDQA02" might vary from line to line since there might be odd number of columns.Can "- From" be taken as a key to cut "- From SMSRun1_GIDQA02" and append it to the front?

Last edited by ragavhere; 07-08-2008 at 07:49 AM.. Reason: Line
# 2  
Old 07-08-2008
With sed:

Code:
sed 's/\(.*\)\( - \)\(.*\)/\3\2\1/' file > newfile

Regards
# 3  
Old 07-08-2008
Thanks. Can you please explain it?

Regards,

Ragav.

Last edited by ragavhere; 07-08-2008 at 08:41 AM.. Reason: Explanation
# 4  
Old 07-08-2008
Quote:
Originally Posted by ragavhere
Thanks. Can you please explain it?

Regards,

Ragav.
Code:
 sed 's/\(.*\)\( - \)\(.*\)/\3\2\1/' file > newfile

With \(.*\) you can select portions of a string. This portion is recalled in the replacement string with \1, \2, \3 etc.

The first portion is the begin of the string until the space before the last minus sign (before " - From").
The second portion are the 3 characters " - " before "From" and the last portion is the rest of the line.
In the replacement string the portion is recalled in the order \3\2\1.

Have a read of a sed tutorial, you can find some links here:

https://www.unix.com/answers-frequent...tutorials.html

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Delimiter appending in a data file if we receive a less columns than expected

Required No.of field = 12 Let say you got a “~” delimited input file and this file has 6 input fields and now I want to add 12-5=7 number of “~” into this input file in order to make it 12 fields datafile can have n number of records ex., a~b~c~d~12~r a~b~c~d~12~r a~b~c~d~12~r... (19 Replies)
Discussion started by: LJJ
19 Replies

2. Shell Programming and Scripting

Appending different columns of multiple files in awk

Hello All, I have three input files cat file1 col1|col2|col3 a|1|A b|2|B cat file2 col1|col2|col3 c|3|C cat file3 col1|col2|col3 d|4|D e|5|E i want below output file4 col1|col2 a|1 (6 Replies)
Discussion started by: looney
6 Replies

3. Shell Programming and Scripting

How to format output in columns by appending multi lines one by one?

Hi, I need to display output in below format Customer : Apr 24 16:31 Customer_Name_111121.txt |---Space---|Apr 24 16:32 Customer_Name _111121. txt |---Space---|Apr 24 16:34 Customer_Name_111112. txt |---Space---|Apr 24 16:35 Customer_Name _222223. txt |---Space---|Apr 24 16:37... (8 Replies)
Discussion started by: ketanraut
8 Replies

4. Shell Programming and Scripting

Match columns and fetch whatever in front of it

Hi Solved these kind of issues using these codes But these are not wrking for my attached files can anybody check........ awk 'NR==FNR{X++;next}{if(X){print}}' file1 file2 awk 'NR==FNR{X=$0;next}{n=split($1,P," ");sub($1,"",$0);for(i=1;i<=n;i++){if(X]){print P,$0}}}' file1 FS="\t" file2 ... (6 Replies)
Discussion started by: Priyanka Chopra
6 Replies

5. UNIX for Dummies Questions & Answers

Appending columns at the end of output using awk/sed

Hi , I have the below ouput, =====gopi===== assasassaa adsadsadsdsada asdsadsadasdsa sadasdsadsd =====kannan=== asdasdasd sadasddsaasd adasdd =====hbk=== asasasssa .... .. I want the output like as below, not able paste here correctly. (2 Replies)
Discussion started by: eeegopikannan
2 Replies

6. Shell Programming and Scripting

Appending columns of two files using shell script

Hi, I am using ksh, I want to read one csv file and append the columns of another file with new column. My input file: col1,col2 --------- siri,886 satya,890 priya,850 Another file with the below date:(test.csv) col3 ----- 321 333 442 (1 Reply)
Discussion started by: siri_886
1 Replies

7. UNIX for Dummies Questions & Answers

Sed $ appending to front, not to the end

I keep trying to append some astrix to the end of a line, but it keeps overwriting at the front of the line. These are the originals Fred Fardbarkle:674-843-1385:20 Parak Lane, Duluth, MN 23850:4/12/23:780900 Fred Fardbarkle:674-843-1385:20 Parak Lane, Duluth, MN 23850:4/12/23:780900 ... (5 Replies)
Discussion started by: DrSammyD
5 Replies

8. Shell Programming and Scripting

appending several columns with awk and paste

Hello, I am trying to solve for a couple of hours now the following problem: I have n files and would like to add the third column of each file to a new file: temp1.txt 1 2 3 1 2 3 1 2 3 temp2.txt 1 2 4 1 2 4 1 2 4 1 2 4 temp3.txt (2 Replies)
Discussion started by: creamcheese
2 Replies

9. Shell Programming and Scripting

Appending columns on a file

My issue is the following: I have several text files, let's say 10 of them. Each one has three columns separated by a tab: Date, Time and Value. What I want to do next is to have only one text file containing the information: Date, Time, Value1, Value2, Value3, ... , Value10, where Value1... (2 Replies)
Discussion started by: abel
2 Replies
Login or Register to Ask a Question