Insert and shifting data at column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert and shifting data at column
# 1  
Old 04-18-2012
Insert and shifting data at column

Hi all,
i have data like this

Code:
joe  :          1          :a
bob :          2          :b
sue            : 3         :c
foo            : 4          :d

at column 2 i want to insert TOP to the top column and at column 3 i want to insert BOTTOM to the bottom column. and the result will like this..

Code:
joe          :TOP            :b
bob:            1                      :c
sue              : 2                       :d
foo              :3              :BOTTOM

how i do that with awk..
thanks for your help
Moderator's Comments:
Mod Comment How to use code tags

Last edited by Scrutinizer; 04-18-2012 at 06:03 AM..
# 2  
Old 04-18-2012
Code:
$ nawk -F\: 'NR==1{f=$0;s=$2}{a[i++]=$0;l=$0;ls=$2;next}END{sub(s," TOP ",f);print f;for(j=1;j<i-1;j++){print a[j]};sub(ls," BOTTOM ",l);print l}' input.txt
joe : TOP :a
bob : 2 :b
sue : 3 :c
foo : BOTTOM :d

# 3  
Old 04-18-2012
Code:
 
user@linux-aah2:~/user/learn> cat data.txt
joe : 1 :a
bob : 2 :b
sue : 3 :c
foo : 4 :d

Script:

Code:
 
#!/bin/ksh
awk -F: -v len=$(wc -l < data.txt) '{
if ( NR==1 )
        print $1":TOP:"$3
else if ( NR==len )
        print $1":"$2":BOTTOM"
else
print
}' data.txt

Output:

Code:
 
joe :TOP:a
bob : 2 :b
sue : 3 :c
foo : 4 :BOTTOM

# 4  
Old 04-18-2012
Quote:
Thanks asteriks...but i want Output like this:

Code:
 
joe :TOP:b
bob : 1 :c
sue : 2 :d
foo : 3 :BOTTOM


Last edited by psychop13; 04-19-2012 at 04:46 AM..
# 5  
Old 04-18-2012
Code:
 
awk -F\: 'NR==1{f=$0;s=$2}{a[i++]=$0;l=$0;ls=$3;next}END{sub(s," TOP ",f);print f;for(j=1;j<i-1;j++){print a[j]};sub(ls," BOTTOM ",l);print l}' input.txt

# 6  
Old 04-18-2012
Quote:
Originally Posted by itkamaraj
Code:
 
awk -F\: 'NR==1{f=$0;s=$2}{a[i++]=$0;l=$0;ls=$3;next}END{sub(s," TOP ",f);print f;for(j=1;j<i-1;j++){print a[j]};sub(ls," BOTTOM ",l);print l}' input.txt

thanks itkamaraj by the result still like asterix script..i want in third column shifting like this
Code:
b
c
d
BOTTOM

# 7  
Old 04-18-2012
No need to make a pass over the file to count lines, nor to cache the data in an array:

Code:
awk -F ":" '
    function p( c2 )
    {
        printf( "%s : %s :", $1, c2 );
    }
    BEGIN {
        getline;
        p( "TOP" );
    }
    {
        printf( "%s\n", $3 );
        p( $2 );
    }
    END {
        printf( "BOTTOM\n" );
    }
' input-file

This User Gave Thanks to agama For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to insert data into black column( Secound Column ) in excel (.XLSX) file using shell script?

Source Code of the original script is down below please run the script and try to solve this problem this is my data and I want it column wise 2019-03-20 13:00:00:000 2019-03-20 15:00:00:000 1 Operating System LAB 0 1 1 1 1 1 1 1 1 1 0 1 (5 Replies)
Discussion started by: Shubham1182
5 Replies

2. Shell Programming and Scripting

Shifting of data because of special characters

Hi Forum. I have a unique problem that I'm hoping someone can assist me. I'm generating a fixed width file and one of the output column (person_name at col. pos.#483 defined as string(36) sometimes contains french characters in the name and it causes the next column of data to shift to the... (10 Replies)
Discussion started by: pchang
10 Replies

3. Shell Programming and Scripting

Change data in one column with data from another file's column

Hello, I have this file outputData: # cat /tmp/outputData __Capacity^6^NBSC01_Licences^L3_functionality_for_ESB_switch __Capacity^2100^NBSC01_Licences^Gb_over_IP __Capacity^1837^NBSC01_Licences^EDGE_BSS_Fnc __Capacity^1816^NBSC01_Licences^GPRS_CS3_and_CS4... (1 Reply)
Discussion started by: nypreH
1 Replies

4. Shell Programming and Scripting

Insert data in first column(if blank) from previous line first column

Dear Team I need to insert field(which is need to taken from previous line's first field) in first column if its blank. I had tried using sed but not find the way. Detail input and output file as below. Kindly help for same. INPUT: SCGR SC DEV DEV1 NUMDEV DCP ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

5. Shell Programming and Scripting

Parsing XML (and insert data) then output data (bash / Solaris)

Hi folks I have a script I wrote that basically parses a bunch of config and xml files works out were to add in the new content then spits out the data into a new file. It all works - apart from the xml and config file format in the new file with XML files the original XML (that ends up in... (2 Replies)
Discussion started by: dfinch
2 Replies

6. Shell Programming and Scripting

Compare 2 files and match column data and align data from 3 column

Hello experts, Please help me in achieving this in an easier way possible. I have 2 csv files with following data: File1 08/23/2012 12:35:47,JOB_5330 08/23/2012 12:35:47,JOB_5330 08/23/2012 12:36:09,JOB_5340 08/23/2012 12:36:14,JOB_5340 08/23/2012 12:36:22,JOB_5350 08/23/2012... (5 Replies)
Discussion started by: asnandhakumar
5 Replies

7. Shell Programming and Scripting

Replace column that matches specific pattern, with column data from another file

Can anyone please help with this? I have 2 files as given below. If 2nd column of file1 has pattern foo1@a, find the matching 1st column in file2 & replace 2nd column of file1 with file2's value. file1 abc_1 foo1@a .... abc_1 soo2@a ... def_2 soo2@a .... def_2 foo1@a ........ (7 Replies)
Discussion started by: prashali
7 Replies

8. Shell Programming and Scripting

Can sed be used to insert data at specific column?

I'm trying to use sed to insert data at a specific column, let's say my data looks like this: 0553 1828 0552 1829 0550 1829 0549 1830 0548 1831 what I want is this: timein 0553 timeout 1828 timein 0552 timeout 1829 timein 0550 timeout 1829 timein 0549 timeout 1830 timein 0548... (5 Replies)
Discussion started by: mswartz
5 Replies

9. Shell Programming and Scripting

Extract data based on match against one column data from a long list data

My input file: data_5 Ali 422 2.00E-45 102/253 140/253 24 data_3 Abu 202 60.00E-45 12/23 140/23 28 data_1 Ahmad 256 7.00E-45 120/235 140/235 22 data_4 Aman 365 8.00E-45 15/65 140/65 20 data_10 Jones 869 9.00E-45 65/253 140/253 18... (12 Replies)
Discussion started by: patrick87
12 Replies

10. Shell Programming and Scripting

How to insert data befor some field in a row of data depending up on values in row

Hi I need to do some thing like "find and insert before that " in a file which contains many records. This will be clear with the following example. The original data record should be some thing like this 60119827 RTMS_LOCATION_CDR INSTANT_POSITION_QUERY 1236574686123083rtmssrv7 ... (8 Replies)
Discussion started by: aemunathan
8 Replies
Login or Register to Ask a Question