Changing part of column value with AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Changing part of column value with AWK
# 1  
Old 09-27-2010
Changing part of column value with AWK

Hi All ,

Here is my req
I am extracting a column value and I want to change part of its value according to user input

Say the column I extracted is of 15 digits and I want to change the 11th to 14th position with user input 4 digit value


Example column value 111111111111111 user input 2345 then o/p will be
111111111123451
is it possible ..
Please help me
# 2  
Old 09-27-2010
bash
Code:
$ var=111111111111111
$ user=2345
$ echo "${var:0:10}${user}${var:15}"
11111111112345

# 3  
Old 09-27-2010
Hi Kurumi,
Thanks a lot but my req is little bit diff
Here is the column value
Code:
1150000100000120100612100000
1151000100000120100612101000
1152000100000120100612102000
1150000100000120100612103000
1151000100000120100612104000
1152000100000120100612105000

and I want to change only the bold part and row values are different
Appreciate your kind help

Last edited by radoulov; 09-27-2010 at 09:14 AM.. Reason: Please use code tags!
# 4  
Old 09-27-2010
Quote:
Originally Posted by Pratik4891
Hi Kurumi,
Thanks a lot but my req is little bit diff
Here is the column value
Code:
1150000100000120100612100000
1151000100000120100612101000
1152000100000120100612102000
1150000100000120100612103000
1151000100000120100612104000
1152000100000120100612105000

and I want to change only the bold part and row values are different
Appreciate your kind help
Suppose your file name is abc.txt and you need 4th column to change


Use the following script with name a.sh
Code:
#!/bin/ksh
nawk '{print $4}' abc.txt >>test.txt
echo "Enter User value:"
read usrvalue
while read line
do
frstpart=`echo $line|cut -c 0-18`
scndpart=`echo $line|cut -c 23-28`
echo "$frstpart$usrvalue$scndpart" >>test1.txt
done <test.txt

test.txt
Code:
1150000100000120100612100000
1151000100000120100612101000
1152000100000120100612102000
1150000100000120100612103000
1151000100000120100612104000
1152000100000120100612105000

execute the script
Code:
 
/.ash
Enter User value:
9999


Your required output file is test1.txt
Code:
 
1150000100000120109999100000
1151000100000120109999101000
1152000100000120109999102000
1150000100000120109999103000
1151000100000120109999104000
1152000100000120109999105000

# 5  
Old 09-27-2010
Code:
$ ruby -pne '$_[18,4]="1234"' file
1150000100000120101234100000
1151000100000120101234101000
1152000100000120101234102000
1150000100000120101234103000
1151000100000120101234104000
1152000100000120101234105000

# 6  
Old 09-27-2010
Hi Kurumi ,

Thanks a lot .

Really appreciate your kind co-operation
Please one more request
how to replace the new column in the original file where all columns will remain unchanged expect the 4th column which will be replaced by the new value you just showed above..Smilie
# 7  
Old 09-27-2010
Code:
ruby -i.bak -pne '$_[3]="1234"' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete part of a column

I want to delete a part of the 4th column from the given file below: <Text Text_ID="10155645315851111_10155645333076543" From="460350337461111" Created="2011-03-16T17:05:37+0000" use_count="123">This is the first text</Text> <Text Text_ID="10155645315851111_10155645317023456"... (2 Replies)
Discussion started by: my_Perl
2 Replies

2. Shell Programming and Scripting

Changing values only in 3rd column and 4th column

#cat file testing test! nipw asdkjasjdk ok! what !ok host server1 check_ssh_disk!102.56.1.101!30!50!/ other host server 2 des check_ssh_disk!192.6.1.10!40!30!/ #grep check file| awk -F! '{print $3,$4}'|awk '{gsub($1,"",$1)}1' 50 30 # Output: (6 Replies)
Discussion started by: kenshinhimura
6 Replies

3. Shell Programming and Scripting

Changing values in the first column.

I have a file which looks like this: 1 3 5 5 3 2 1 5 3 6 3 2 0 5 6 3 2 9 0 4 5 6 4 9 In the first column, instead of 1, I want to place +1 and instead of 0, I want -1 to be put. So the generated file should look like this: +1 3 5 5 3 2 +1 5 3 6 3 2 -1 5 6 3 2 9 -1 4 5 6 4 9 Just to... (9 Replies)
Discussion started by: shoaibjameel123
9 Replies

4. Shell Programming and Scripting

Grep/awk part of info of a column

I have a question that I am at a loss to solve. I have 3 column tab-separated data, such as: abs nmod+n+n-commitment-n 349.200023 abs nmod+n+n-a-commitment-n 333.306429 abs into+ns-j+vn-pass-rb-divide-v 295.57316 abs nmod+n+ns-commitment-n 182.085018 abs nmod+n+n-pledge-n ... (2 Replies)
Discussion started by: owwow14
2 Replies

5. Shell Programming and Scripting

Use grep/awk to remove part of column

hi all, how can i use grep or awk to clean the following input data: n<>the<>96427210 861521305 123257583 n<>obj<>79634223 861521305 79634223 n<>nmod<>68404733 861521305 68422718 where the desired results is to remove all non-numeric characters?: 96427210 861521305 123257583 ... (5 Replies)
Discussion started by: owwow14
5 Replies

6. UNIX for Dummies Questions & Answers

Changing the values of a column using awk and gsub

Hi, I am using the following code to change NA to X in only the 5th column of my text file: awk '{gsub("NA","x",$5)}1' in.file > out.file How can I modify this code if I want to change NA to X in multiple columns of the text file (i.e. columns 5,6 and 7). Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

7. Shell Programming and Scripting

awk to replace part of a column

dear all, I'm trying to use Awk to eliminate the last two characters from the first column in a file. This two characters are "-1" and I need to eliminate them from each row that I have in the files. The files have two columns and look like: ID_090-1 2 ID_3787-1 4 ID_0098-1 1 ID_12-1 4 I... (4 Replies)
Discussion started by: gabrysfe
4 Replies

8. Shell Programming and Scripting

Awk: Need help replacing a specific column in a file by part of a column in another file

Hi, I have two input files as File1 : ABC:client1:project1 XYZ:client2-aa:project2 DEF:client4:proj File2 : client1:W-170:xx client2-aa:WT-04:yy client4:L-005A:zz Also, array of valid values can be hardcoded like Output : ABC:W:project1 XYZ:WT:project2 (1 Reply)
Discussion started by: aa2601
1 Replies

9. UNIX for Dummies Questions & Answers

Changing part of file name

I have a bunch of files that got mislabeled, something like this: An d I need to change the first part of the file name for a different identifier, something like this: How can I accomplish such task? Thanks! (2 Replies)
Discussion started by: Xterra
2 Replies

10. 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
Login or Register to Ask a Question