Conditional replacement of columns in a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conditional replacement of columns in a text file
# 1  
Old 01-21-2014
Conditional replacement of columns in a text file

Hello scriping expert friends,

I have 2 requirements on replacing fields of text files:

I have lot of data with contents like below:
Code:
[FEN "1rr2b1k/3q1P1p/p4p2/1pp1p2B/2PpP2P/1P1P4/P3Q3/R1B3RK w - - 0 33"]
[FEN "2bqk1r1/p4p2/1p2pQp1/1n1pP1Bp/7P/3P2N1/P4PP1/6K1 w - - 0 3"]
[FEN "2q3kr/6r1/1p2N3/5p2/1P1p4/6R1/7P/6K1 w - - 0 3"]
[FEN "2r2r1k/pp1q1p1p/3bpn2/5P2/1P6/P3P3/1B3P1P/R3K1R1 w Q - 0 2"]

Requirement-1:

The digit after 0 should always be changed to 1
Code:
[FEN "1rr2b1k/3q1P1p/p4p2/1pp1p2B/2PpP2P/1P1P4/P3Q3/R1B3RK w - - 0 1"]
[FEN "2bqk1r1/p4p2/1p2pQp1/1n1pP1Bp/7P/3P2N1/P4PP1/6K1 w - - 0 1"]
[FEN "2q3kr/6r1/1p2N3/5p2/1P1p4/6R1/7P/6K1 w - - 0 1"]
[FEN "2r2r1k/pp1q1p1p/3bpn2/5P2/1P6/P3P3/1B3P1P/R3K1R1 w Q - 0 1"]

Requirement-2:

if 3rd column value is "w" (just before hyphens) then I should make it to "b" and vice-versa.
Code:
[FEN "1rr2b1k/3q1P1p/p4p2/1pp1p2B/2PpP2P/1P1P4/P3Q3/R1B3RK b - - 0 33"]
[FEN "2bqk1r1/p4p2/1p2pQp1/1n1pP1Bp/7P/3P2N1/P4PP1/6K1 b - - 0 3"]
[FEN "2q3kr/6r1/1p2N3/5p2/1P1p4/6R1/7P/6K1 b - - 0 3"]
[FEN "2r2r1k/pp1q1p1p/3bpn2/5P2/1P6/P3P3/1B3P1P/R3K1R1 b Q - 0 2"]

I could get them done but with some poor scripting. I read value of each column and compare and echoing to another file but I guess awk/sed would do so much effecient. I have no idea how to use awk.Smilie

Please provide me 2 different commands separately as sometimes I may have to do only one of them.

TIA
# 2  
Old 01-21-2014
Code:
$ awk 'sub(/[[:digit:]]+/,new,$NF)' new="1" file
[FEN "1rr2b1k/3q1P1p/p4p2/1pp1p2B/2PpP2P/1P1P4/P3Q3/R1B3RK w - - 0 1"]
[FEN "2bqk1r1/p4p2/1p2pQp1/1n1pP1Bp/7P/3P2N1/P4PP1/6K1 w - - 0 1"]
[FEN "2q3kr/6r1/1p2N3/5p2/1P1p4/6R1/7P/6K1 w - - 0 1"]
[FEN "2r2r1k/pp1q1p1p/3bpn2/5P2/1P6/P3P3/1B3P1P/R3K1R1 w Q - 0 1"]

$ awk '{$(NF-4)=new}1' new="b" file
[FEN "1rr2b1k/3q1P1p/p4p2/1pp1p2B/2PpP2P/1P1P4/P3Q3/R1B3RK b - - 0 33"]
[FEN "2bqk1r1/p4p2/1p2pQp1/1n1pP1Bp/7P/3P2N1/P4PP1/6K1 b - - 0 3"]
[FEN "2q3kr/6r1/1p2N3/5p2/1P1p4/6R1/7P/6K1 b - - 0 3"]
[FEN "2r2r1k/pp1q1p1p/3bpn2/5P2/1P6/P3P3/1B3P1P/R3K1R1 b Q - 0 2"]

This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 01-21-2014
Thank you Akshay Hegde,

First one works great.

Second one always makes it to "b". As I said in my first post, we need it vice-versa. For ex. it should change to "w" if it already has "b" and it should change to "b" if it has "w".

Thanks a lot!
# 4  
Old 01-21-2014
Okay this would be fine, assuming possibilities are only either "b" or "w"

Code:
$ awk '{$(NF-4)=$(NF-4)=="b"?"w":"b"}1' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple Replacement in a Text File in one operation (sed/awk) ?

Hi all, Saying we have two files: 1. A "Reference File" whose content is "Variable Name": "Variable Value" 2. A "Model File" whose content is a model program in which I want to substitute "VariableName" with their respective value to produce a third file "Program File" which would be a... (4 Replies)
Discussion started by: dae
4 Replies

2. Shell Programming and Scripting

Conditional replacement in CSV files

Hello, I have many CSV files with variable number of rows and columns. Sample of few problematic CSV files. ,,Price,Price,Price,Price,Price,Price,Price,Price,Price,Qty Date,Sl,AAA,BBB,CCC,DDD,EEE,FFF,GGG,HHH,PriQueue,%busy 30/07/2014,1,AAA,BBB,CCC,DDD,EEE,FFF,GGG,HHH,NA,0... (8 Replies)
Discussion started by: reddyr
8 Replies

3. Shell Programming and Scripting

How to concatenate 2-columns by 2 -columns for a text file?

Hello, I want to concatenate 2-columns by 2-columns separated by colon. How can I do so? For example, I have a text file containing 6 columns separated by tab. I want to concatenate column 1 and 2; column 3 and 4; column 5 and 6, respectively, and put a colon in between. input file: 1 0 0 1... (10 Replies)
Discussion started by: huiyee1
10 Replies

4. UNIX for Dummies Questions & Answers

Removing columns from a text file that do not have any values in second and third columns

I have a text file that has three columns. But at the end of the text file, there are trailing lines that have missing second and third columns: 4 0.04972604 KLHL28 4 0.0497332 CSTB 4 0.04979822 AIF1 4 0.04983331 DECR2 4 0.04990344 KATNB1 4 4 4 4 How can I remove the trailing... (3 Replies)
Discussion started by: evelibertine
3 Replies

5. Shell Programming and Scripting

Conditional replacement of a delimiter

Hello, I'm new to this forum but this seems like the place to ask this question. I have a pipe delimited data file with the fields except for the header being encased in double quotes. I found out that some of the fields have an trash pipe within the data itself. I'd like to conditionally... (4 Replies)
Discussion started by: samahs
4 Replies

6. Shell Programming and Scripting

conditional replacement

Hi all, I need a bash, sed, awk script or one liner to do the following task: This is the format of a text file: 2010-06-11 20:01 902656 HOP-W-100412-1.doc 2010-11-05 18:01 364447 NEX-W-101104-1 2010-07-06 10:01 64512 Cerintele 2010-07-06 10:01 599420 content 2010-07-19 14:01 1785344... (7 Replies)
Discussion started by: supervazi
7 Replies

7. UNIX for Dummies Questions & Answers

How to convert text to columns in tab delimited text file

Hello Gurus, I have a text file containing nearly 12,000 tab delimited characters with 4000 rows. If the file size is small, excel can convert the text into coloumns. However, the file that I have is very big. Can some body help me in solving this problem? The input file example, ... (6 Replies)
Discussion started by: Unilearn
6 Replies

8. Shell Programming and Scripting

Conditional tab replacement sed/awk

Hi I am struggling to find a solutions to this problem: I have a directory full of files and I wish to: read each line of each file and if any one line in those files is longer than 72 characters I want to replace any tab characters with a space character. Ive been... (3 Replies)
Discussion started by: benackland
3 Replies

9. Shell Programming and Scripting

HELP Need in SED/PERL conditional line replacement

Hi , I need some help on perl/sed conditional replacement The situation is like below . I have a file contents like below . AAA|BBB|CCC|DDD AAA|BCF|CCC|HHH AAA|BVF|JJJ|KKK Here in the above file . I know my second column value (taking "|" as my delimited ) Basically I have to... (3 Replies)
Discussion started by: robin.r888
3 Replies

10. Shell Programming and Scripting

Replacement of text in a file

Hi , I have some data in my file(properties.txt) like this. # agent.properties agent.dmp.Location= agent.name= I need to relpace the agent.dmp.location with agent.dmp.Location = /opt/VRTS/vxvm I am using the follwing to replace the string AGENT_NAME=snmp... (2 Replies)
Discussion started by: raghu.amilineni
2 Replies
Login or Register to Ask a Question