Visit Our UNIX and Linux User Community


To trim Certain field in a line of a file and replace the new string in that position


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To trim Certain field in a line of a file and replace the new string in that position
# 1  
Old 08-14-2009
Lightbulb To trim Certain field in a line of a file and replace the new string in that position

To trim 3rd field in for all the lines of a file and replace the modified string in that particular field.

For example i have a file called Temp.txt having content
Temp.txt
-----------------
100,234,M1234
400,234,K1734
300,345,T3456
----------------

So the modified file output should be
Temp.txt
-----------------
100,234,1234
400,234,1734
300,345,3456
----------------

Soplution: I did like this

while read line
do
VAR=`echo $line | cut -f3 -d ','` ( VAR=M1234)
VAR1=`echo $VAR | cut -c2-` (VAR1=1234)
???????????????????????????
??????????????????????????

done < Temp.txt

So please help me how i will replace VAR1 inplace of VAR and got the output as i specified above..

Thanks in advance...

Last edited by Franklin52; 08-14-2009 at 07:01 AM.. Reason: E-mail adrress removed, all Q&A should take place in the forums
# 2  
Old 08-14-2009
Hi.

You can use Awk:

Code:
awk -v FS="," -v OFS="," '{$3 = substr($3, 2)} 1' Test.txt


Last edited by Scott; 08-14-2009 at 07:35 AM..
# 3  
Old 08-14-2009
If you are freak of sed this will do..
Code:
sed 's/\(.*\),\(.*\),[aA-zZ]\(.*\)/\1,\2,\3/g' filename

# 4  
Old 08-14-2009
Shorter:

Code:
sed 's/[a-zA-Z]//g' file

# 5  
Old 08-15-2009
And shell version
Code:
#!/bin/ksh
# or bash
while IFS="," read f1 f2 f3 fx
do
       new3=${f3:1}
       echo "$f1,$f2,$new3"
done <  Temp.txt


Previous Thread | Next Thread
Test Your Knowledge in Computers #474
Difficulty: Easy
Binary numbers are expressed in terms of a power of 2 instead of a power of 10.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

2. UNIX for Dummies Questions & Answers

How do I replace a string in file that is in a certain position with spaces?

I am trying to replace the string in position 26 through 35 of the data file with 10 spaces and I want the remaining file to stay as is, the record length is over 900 characters? I am trying to use the AWK and substr but I am not getting it formatted correctly. Before... (6 Replies)
Discussion started by: fnwine1500
6 Replies

3. Shell Programming and Scripting

Search for a string at a particular position and replace with blank based on position

Hi, I have a file with multiple lines(fixed width dat file). I want to search for '02' in the positions 45-46 and if available, in that lines, I need to replace value in position 359 with blank. As I am new to unix, I am not able to figure out how to do this. Can you please help me to achieve... (9 Replies)
Discussion started by: Pradhikshan
9 Replies

4. Shell Programming and Scripting

Replace a field with line number in file

I am working on a script to convert bank data to a csv file. I have the format done - columns etc. The final piece of the puzzle is to change the second field (after the R) of every line to reflect its' line number in the file. I am stumped. I can use awk on each line but need help looping through... (9 Replies)
Discussion started by: Melah Gindi
9 Replies

5. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

6. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

7. Shell Programming and Scripting

Search a string,get line and replace with second field

Hi, I need to search for source path in file2 , as per file1 and if found get the next line and take the field value and put it in URL value of file1. In file1, NF is not same for all the lines. file1: <type source="/home/USER/Desktop" Dest="/home/USER/DIR1/Desktop" URL="ssh/path"/> <type... (8 Replies)
Discussion started by: greet_sed
8 Replies

8. UNIX for Dummies Questions & Answers

Search a string in the file and then replace another string after that position

Hi I am looking for a particular string in a file.If the string exists, then I want to replace another string with some other text.Once replaced, search for the same text after that character position in the file. :wall: E.g: Actual File content: Hello Name: Nitin Raj Welcome to Unix... (4 Replies)
Discussion started by: dashing201
4 Replies

9. UNIX for Dummies Questions & Answers

Search for a string and replace the searched string in the same position in samefile

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found in the same file..The... (27 Replies)
Discussion started by: ganesh_248
27 Replies

Featured Tech Videos