Visit Our UNIX and Linux User Community


Find and Replace random numeric value with non-numeric value


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Find and Replace random numeric value with non-numeric value
# 1  
Old 08-18-2010
Find and Replace random numeric value with non-numeric value

Can someone tell me how to change the first column in a very large 17k line file from a random 10 digit numeric value to a non numeric value. The format of lines in the file is:
1702938475,SNU022,201004

the first 10 numbers always begin with 170
# 2  
Old 08-18-2010
Code:
echo "1702938475,SNU022,201004" |awk  'BEGIN{FS=OFS=","} /^170/ {$1="ABCDEFG"}1'

# 3  
Old 08-19-2010
---------- Post updated at 11:23 PM ---------- Previous update was at 11:00 PM ----------

Quote:
Originally Posted by rdcwayx
Code:
echo "1702938475,SNU022,201004" |awk  'BEGIN{FS=OFS=","} /^170/ {$1="ABCDEFG"}1'

The file has 17,000+ records(rows) of data with the following format,

1702938475,SNU022,201004

but the first column number above in red, is different for every row, the 7 numbers that follow 170 are random numbers for each row. So do i still echo that string?
# 4  
Old 08-19-2010
sed -i s/^170[0-9][0-9][0-9][0-9][0-9][0-9][0-9]/<PATTERN>/g file.txt
This User Gave Thanks to deepikad For This Post:
# 5  
Old 08-19-2010
Quote:
Originally Posted by Bahf1s
[SIZE=1]

but the first column number above in red, is different for every row, the 7 numbers that follow 170 are random numbers for each row. So do i still echo that string?
No, I think that rdcwayx was giving an example of how you might go about it using the echo as a sample for testing.

I think this is the most straight forward way:

Code:
sed 's/^[^,]*/aaaaaaaaaa/' input-file-name >output-file

This will match everything from the beginning of the line to the first comma (the 10 digit number) and replace it with 'aaaaaaaaaa' You can replace the string of a's with what ever you need.
This User Gave Thanks to agama For This Post:
# 6  
Old 08-19-2010
Quote:
Originally Posted by Bahf1s
---------- Post updated at 11:23 PM ---------- Previous update was at 11:00 PM ----------



The file has 17,000+ records(rows) of data with the following format,

1702938475,SNU022,201004

but the first column number above in red, is different for every row, the 7 numbers that follow 170 are random numbers for each row. So do i still echo that string?
Code:
awk  'BEGIN{FS=OFS=","} /^170/ {$1="ABCDEFG"}1' urfile

This User Gave Thanks to rdcwayx For This Post:
# 7  
Old 08-19-2010
Thank you very much Agama, this worked perfectly!!

---------- Post updated at 10:51 AM ---------- Previous update was at 10:47 AM ----------

Thank you very much Agama, this worked perfectly!!!
-----------------------------------------------------

Quote:
Originally Posted by agama
No, I think that rdcwayx was giving an example of how you might go about it using the echo as a sample for testing.

I think this is the most straight forward way:

Code:
sed 's/^[^,]*/aaaaaaaaaa/' input-file-name >output-file

This will match everything from the beginning of the line to the first comma (the 10 digit number) and replace it with 'aaaaaaaaaa' You can replace the string of a's with what ever you need.


---------- Post updated at 10:53 AM ---------- Previous update was at 10:51 AM ----------

Thanks to everyone who provided input and new knowledge.
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #822
Difficulty: Medium
In HTML5, both the strike and div elements were deprecated.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace a numeric values in a certain column

Hi All, I am trying to replace a certain value from one place in a file . In the below file at position 35 I will have 8 I need to modify all 8 in that position to 7 I tried awk '{gsub("8","7",$35)}1' infile > outfile ----> not working sed -i 's/8/7'g' infile --- it is replacing all... (3 Replies)
Discussion started by: arunkumar_mca
3 Replies

2. Shell Programming and Scripting

UNIX-how to find out the numeric value

Hi All, I need to found the below numeric value using unix(shell) commands in .txt file and its available in root directory. for example: file name is siva.txt and content is Siva_12345_werwerwe,11,22,33 Siva_12346_werwerwe,22,33,44 Siva_12347_werwerwe,55,44,66 for above string i... (2 Replies)
Discussion started by: sivaraman1981
2 Replies

3. Shell Programming and Scripting

Find the numeric value in a string and then check the max. value

hi, i have a string " 00000069 ThreadMonitor W WSVR0606W: Thread "WebContainer : 43|null" (00000069) was previously rep orted to be hung but has completed. It was active for approximately 47533430 milliseconds. There is/are 43 thread(s) in tot al in the server that still may be hung." ... (4 Replies)
Discussion started by: lovelysethii
4 Replies

4. Shell Programming and Scripting

find numeric duplicates from 300 million lines....

these are numeric ids.. 222932017099186177 222932014385467392 222932017371820032 222932017409556480 I have text file having 300 millions of line as shown above. I want to find duplicates from this file. Please suggest the quicker way.. sort | uniq -d will... (3 Replies)
Discussion started by: pamu
3 Replies

5. UNIX for Dummies Questions & Answers

SED command to search for numeric and replace

Hi I am very new to linux and scripting. I need to replace numbers abc with number xyz inputting from a reference file. I used the following command - sed "s/$grd/$lab/" , where $grd and $lab comes from reference file. The problem is the above line doesnt take care of space..... (1 Reply)
Discussion started by: ajayh
1 Replies

6. Shell Programming and Scripting

Replace variable length numeric string

I have a customer who logged some cc and bank account numbers in their apache logs. I got the cc numbers x'd out with sed -e 's/args=\{16\}/args=XXXXXXXXXXXXXXXX/g' -e 's/cardnum=\{16\}/cardnum=XXXXXXXXXXXXXXXX/g'but that wasn't too difficult due to the value being 16 digits. The bank account... (7 Replies)
Discussion started by: mk4mzid
7 Replies

7. Shell Programming and Scripting

Numeric or not?

Is there an easy way using a command or other routine for testing whether an argument on the command line is numeric? Just want to validate... I ran a search and didn't find a similar question.... Thx (3 Replies)
Discussion started by: harrisjl
3 Replies

8. Shell Programming and Scripting

Numeric or not

Is there a simple way of determining whether a command line arguement is numeric or not?? I tried a search and didn't find anything similar... This is the 2nd time I've made this post.... It didn't appear that the first one showed up ?? Sorry if this is a duplicate... (1 Reply)
Discussion started by: harrisjl
1 Replies

9. Shell Programming and Scripting

Perl code to differentiate numeric and non-numeric input

Hi All, Is there any code in Perl which can differentiate between numeric and non-numeric input? (11 Replies)
Discussion started by: Raynon
11 Replies

10. Shell Programming and Scripting

Replace spaces with 0's having numeric values.

What could be the regular expression with gsub function in awk to replace all numerics having spaces before to be replaced with 0s? (1 Reply)
Discussion started by: videsh77
1 Replies

Featured Tech Videos