Get Column location Dynamically


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get Column location Dynamically
# 1  
Old 03-18-2011
Get Column location Dynamically

Code:
File Format

U	A	45	B
N	A	78	M	
M 	A	98	O

Need to search for A in the file and whichever location is A is located need to get its next column i.e need to get 45 78 and 98 and put these values at end of file as new column


Output

Code:
U	A	45	B	45
N	A	78	M	78
M 	A	98	O	98

I can do this via a while loop and all the clutter in shell script but i need a efficient and clean way to achieve this
# 2  
Old 03-18-2011
Code:
awk '{if($2=="A")print $1" " $2" " $3" " $4" " $3;else print $1" " $2" " $3" " $4}' filename

# 3  
Old 03-18-2011
Hi dinjo_jo,

The sample has tab as field separator, the try:
Code:
awk '{for(i=1;i<=NF;++i)if($i=="A")print $0"\t"$(i+1)}' inputfile
U    A    45    B    45
N    A    78    M    78
M    A    98    O    98

If it has space as field separator remove the "\t" and use:
Code:
awk '{for(i=1;i<=NF;++i)if($i=="A")print $0,$(i+1)}' inputfile

Best regards.
# 4  
Old 03-18-2011
How about this?
Code:
awk '{sub($0,$0 FS $3)}1' file

# 5  
Old 03-18-2011
cgkmal it works.

How do i identify at which column the match was found ? So that i can do some more processing

Last edited by dinjo_jo; 03-18-2011 at 02:51 AM..
# 6  
Old 03-18-2011
Quote:
Originally Posted by dinjo_jo
cgkmal it works.

How do i identify at which column the match was found ? So that i can do some more processing
Code:
awk '{for(i=1;i<=NF;++i)if($i=="A")print $0,$(i+1)}' inputfile

The awk script searches the pattern "A" in every line, then:
Code:
for(i=1;i<=NF;++i)--> The "for loop" goes through all the columns/fields (NF=last column number, in this case NF=4) for every line. 

Code:
if($i=="A") --> Within the "for loop" is present the condition that says, when I pass for column i, and is found that column i has 
a value ="A" ($i="A"),

Code:
print $0,$(i+1) --> then, inmediately print the whole line ($0), followed by an space "," and the value after column i =$(i+1)



If you want to know the column number in which match was found, simply remove the "$" symbol from
$i and you will have it, something like:
Code:
awk '{for(i=1;i<=NF;++i)if($i=="A")print "string A, was found in column " i}' inputfile


$i=Gives the content/value of the column, ($3= Content of column 3=45 or 78 or 98, depends line number);
i = Gives the number of the column, (3=Number of column 3=3).

Hope it helps,

Best regards

Last edited by cgkmal; 03-18-2011 at 03:31 AM..
# 7  
Old 03-18-2011
Very well explained , top notch
This User Gave Thanks to dinjo_jo For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with copying the list of files from one location to other location

A) I would like to achive following actions using shell script. can someone help me with writing the shell script 1) Go to some dir ( say /xyz/logs ) and then perform find operation in this dir and list of subdir using find . -name "*" -print | xargs grep -li 1367A49001CP0162 >... (1 Reply)
Discussion started by: GG2
1 Replies

2. Shell Programming and Scripting

How to find a existing file location and directory location in Solaris box?

Hi This is my third past and very impressed with previous post replies Hoping the same for below query How to find a existing file location and directory location in solaris box (1 Reply)
Discussion started by: buzzme
1 Replies

3. Shell Programming and Scripting

How to copy a file from one location to another location?

I have file file1.txt in location 'loc1'. Now i want a copy of this file in location 'loc2' with a new file called test.txt. Please help me how to do this in shell script. (1 Reply)
Discussion started by: vel4ever
1 Replies

4. Shell Programming and Scripting

File created in a different location instead of desired location on using crontab

Hi, I am logging to a linux server through a user "user1" in /home directory. There is a script in a directory in 'root' for which all permissions are available including the directory. This script when executed creates a file in the directory. When the script is added to crontab, on... (1 Reply)
Discussion started by: archana.n
1 Replies

5. Shell Programming and Scripting

Help on Moving files from one location to another location

Hi, I am new to unix and shell scripting. Please help me in resolving the below issue. In my shell script I have a variable which stores the different files with the path. Now I need to move all the files one by one to another location. ---- 1.... (4 Replies)
Discussion started by: kpagadala
4 Replies

6. Shell Programming and Scripting

Shell Script for Copy files from one location to another location

Create a script that copies files from one specified directory to another specified directory, in the order they were created in the original directory between specified times. Copy the files at a specified interval. (2 Replies)
Discussion started by: allways4u21
2 Replies

7. Shell Programming and Scripting

Put one string from one location to another location in a file

Hi Everyone, I have 1.txt here a b c' funny"yes"; d e The finally output is: here a b c d e' funny"yes"; (1 Reply)
Discussion started by: jimmy_y
1 Replies

8. Shell Programming and Scripting

Changing one column of delimited file column to fixed width column

Hi, Iam new to unix. I have one input file . Input file : ID1~Name1~Place1 ID2~Name2~Place2 ID3~Name3~Place3 I need output such that only first column should change to fixed width column of 15 characters of length. Output File: ID1<<12 spaces>>Name1~Place1 ID2<<12... (5 Replies)
Discussion started by: manneni prakash
5 Replies

9. Shell Programming and Scripting

Transfer files from one location to another location

Hi, i have to transfer of files of User1 located in Location1 to user2 located in Location2 using shell script. Please suggest me. (1 Reply)
Discussion started by: KiranKumarKarre
1 Replies

10. UNIX for Advanced & Expert Users

copy files from one location to similar location

I need help in forming a script to copy files from one location which has a sub directory structure to another location with similar sub directory structure, say location 1, /home/rick/tmp_files/1-12/00-25/ here 1-12 are the number of sub directories under tmp_files and 00-25 are sub... (1 Reply)
Discussion started by: pharos467
1 Replies
Login or Register to Ask a Question