Replace 2nd column in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace 2nd column in file
# 8  
Old 12-17-2012
Code:
nawk ' { for(i=77;i<=101;i++) { if($1=="TSCmerge"i".Merge.out.remoteOn") { $2="\t\t\tFALSE\t\t\t"; print $0; } } } ' /tmp/test

This User Gave Thanks to Yoda For This Post:
# 9  
Old 12-17-2012
Thank you bipinajith, it is working Smilie
i will modify it according to my need. Because file has other entries too.
# 10  
Old 12-17-2012
See post https://www.unix.com/302738929-post7.html
Adapted to your new requirements, it would look like
Code:
awk 'BEGIN{OFS=FS="\t"}
     /TSCmerge(77|78|80)/ {$4=$4=="TRUE"?"FALSE":"TRUE"}
     1
    '  file
TSCmerge77.Merge.out.remoteOn            FALSE            write
TSCmerge78.Merge.out.remoteOn            FALSE            write
TSCmerge79.Merge.out.remoteOn            TRUE             write
TSCmerge80.Merge.out.remoteOn            FALSE            write
TSCmerge100.Merge.out.remoteOn           TRUE             write
TSCmerge101.Merge.out.remoteOn           TRUE             write


Last edited by RudiC; 12-17-2012 at 04:03 AM..
This User Gave Thanks to RudiC For This Post:
# 11  
Old 12-17-2012
RudiC, the link seems to be broken.
# 12  
Old 12-17-2012
Sorry, repaired it in place.
This User Gave Thanks to RudiC For This Post:
# 13  
Old 12-17-2012
Its working now.
# 14  
Old 12-17-2012
Given your sample input and your for loop, a trivial way to get what you want is:
Code:
sed -e 's/ *TRUE */                     FALSE                   /' /tmp/test

Note that there are three tab characters before and after the FALSE in the above substitution.

Note also that there are no tab characters in your input and the number of spaces in your input don't match what would be produced if the tabs had been converted to spaces, but this gives what you said you want given what you said is in your input file.

If you have a file that has a list of values you want to match in field 1 of your input lines in /tmp/test (in a file named keys in the following examples) one of the following examples may do what you want. They provide different ways of handling spacing around TRUE and FALSE, printing only matched lines or printing all lines, and changing or preserving existing spacing
Code:
#!/bin/ksh
echo "Use sed to change TRUE surrounded by spaces to 3 tabs FALSE 3 tabs"
sed -e 's/ *TRUE */                     FALSE                   /' /tmp/test
echo
echo "Use awk to change TRUE to FALSE on lines matching given keys and
print changed lines.  (Spaces and tabs are not changed.)"
awk 'FNR==NR{
        m[$1]
        next
}
substr($1, 1, index($1, ".") - 1) in m {
        sub(/TRUE/, "FALSE")
        print
}' keys /tmp/test
echo
echo "Use awk to change TRUE to FALSE on lines matching given keys and
print changed lines.  (Each set of spaces and tabs is changed to 3 tabs.)"
awk 'FNR==NR{
        m[$1]
        next
}
substr($1, 1, index($1, ".") - 1) in m {
        sub(/[ \t]*TRUE[ \t]*/, "\t\t\tFALSE\t\t\t")
        print
}' keys /tmp/test
echo
echo "Use awk to change TRUE to FALSE on lines matching given keys and
print all lines.  (Each set of spaces and tabs on lines that have been
changed is changed to 3 tabs.)"
awk 'FNR==NR{
        m[$1]
        next
}
substr($1, 1, index($1, ".") - 1) in m {
        sub(/[ \t]*TRUE[ \t]*/, "\t\t\tFALSE\t\t\t")
}
{       print
}' keys /tmp/test
echo
echo "Use awk to change TRUE to FALSE on lines matching given keys and
print all lines.  (Each set of paces and tabs is changed to 3 tabs.)"
awk 'FNR==NR{
        m[$1]
        next
}
{       if(substr($1, 1, index($1, ".") - 1) in m)
                sub(/[ \t]*TRUE[ \t]*/, "\t\t\tFALSE\t\t\t")
        else    sub(/[ \t]*TRUE[ \t]*/, "\t\t\tTRUE\t\t\t")
        print
}' keys /tmp/test

This was tested with the file keys containing the following three lines:
Code:
TSCmerge77
TSCmerge100
TSCmerge101

I used ksh in my tests, but any shell that supports basic Bourne shell syntax should work fine. This was tested on OS X. If you're using a Solaris system, use nawk or /usr/xpg4/bin/awk instead of awk.
This User Gave Thanks to Don Cragun 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

Compare 1st column from 2 file and if match print line from 1st file and append column 7 from 2nd

hi I have 2 file with more than 10 columns for both 1st file apple,0,0,0...... orange,1,2,3..... mango,2,4,5..... 2nd file apple,2,3,4,5,6,7... orange,2,3,4,5,6,8... watermerlon,2,3,4,5,6,abc... mango,5,6,7,4,6,def.... (1 Reply)
Discussion started by: tententen
1 Replies

2. Shell Programming and Scripting

Match column 8 in file 1 with column 2 in file 2 and replace..

I am looking at the NR==FNR posts and trying to use them to achieve the following but I am not getting it. I have 2 files. I want to match column 8 in file 1 with column 2 in file 2. When they match I want to replace column 9 in file 1 with column 1 in file 2. This is and extract from file 1 ... (5 Replies)
Discussion started by: kieranfoley
5 Replies

3. Shell Programming and Scripting

Replace 2nd Column

Dear All, I have data like this, input.txt A 0B 1828 C 100D 1666,C 200D 1727,C 300D 1783, A 0B 1786 C 200D 1727,C 100D 1666,C 300D 1783, C 400D 1812,C 600D 1869,C 500D 1841, C 400D 1812,C 500D 1841,C 600D 1869, A 0B 1349 C 200D 1727,C 300D 1783,C 100D... (1 Reply)
Discussion started by: attila
1 Replies

4. Shell Programming and Scripting

Change the text in 2nd column in file

Legends, I have following entries in a file. I want to toggle the entry of 2nd column. TRUE/FALSE i tried using perl -p -i -e but the problem is, if instead of "tab" space after 1st column, there is a character or multi character space then this won't work. perl -p -i -e... (6 Replies)
Discussion started by: sdosanjh
6 Replies

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

6. Shell Programming and Scripting

Replace 2nd column for each line in a csv file with fixed string+random number

Hi experts, My csv file looks like this U;cake;michael;temp;;;; U;bread;john;temp;;;; U;cocktails;sarah;temp;;;; I'd like to change the value fo 2nd column to cf+random number , which will look maybe something like this U;cf20187;michael;temp;;;; U;cf8926;john;temp;;;;... (7 Replies)
Discussion started by: tententen
7 Replies

7. Shell Programming and Scripting

Replace 2nd column of CSV file with numbers on line

I have a csv file with occasional multiple entries in the second column. 111111,104,07-24-2011,3.15,N, 222222,020 140,07-24-2011,10.00,N,I want the result 111111,104,07-24-2011,3.15,N, 222222,020,07-24-2011,10.00,N, 222222,140,07-24-2011,10.00,N, I know I can get the output of the second... (5 Replies)
Discussion started by: ffdstanley
5 Replies

8. Shell Programming and Scripting

comparing column of two different files and print the column from in order of 2nd file

Hi friends, My file is like: Second file is : I need to print the rows present in file one, but in order present in second file....I used while read gh;do awk ' $1=="' $gh'" {print >> FILENAME"output"} ' cat listoffirstfile done < secondfile but the output I am... (14 Replies)
Discussion started by: CAch
14 Replies

9. Shell Programming and Scripting

Replace column with column from another file

Hello, I am trying to replace the column in file1 with the column from file2. The two files will have the same amount of rows. Each row will correspond with the same row in the other file. File1 "Replace this column" 500 13-APR-2011... (11 Replies)
Discussion started by: doobe01
11 Replies

10. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies
Login or Register to Ask a Question