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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace 2nd column for each line in a csv file with fixed string+random number
# 1  
Old 10-27-2011
Data Replace 2nd column for each line in a csv file with fixed string+random number

Hi experts,

My csv file looks like this
Code:
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
Code:
U;cf20187;michael;temp;;;;
U;cf8926;john;temp;;;;
U;cf1009;sarah;temp;;;;

Also it may consists more than 3 lines.
Smilieplease help thanks!

Last edited by Franklin52; 10-27-2011 at 08:29 AM.. Reason: Please use code tags, thank you
# 2  
Old 10-27-2011
Code:
awk -F\; 'BEGIN {srand()} {sub ($2,"cf"int(rand()*100000)); print}' infile.csv

where 100000 is whatever maximum value you want.

If you want the same sequence every time just remove the srand() section:
Code:
awk -F\; '{sub ($2,"cf"int(rand()*100000)); print}' infile.csv

# 3  
Old 10-27-2011
Code:
awk -F';' '
function randint(n) {
  return int(n * rand())
}
BEGIN {          
  OFS=";"
  srand()                     
}                           
{ $2 = "cf" randint(65535) }1          
' INPUTFILE
U;cf31289;michael;temp;;;;
U;cf26763;john;temp;;;;
U;cf50994;sarah;temp;;;;

# 4  
Old 10-27-2011
Dear carloM and yazu,

Thank you for your replies. However I am getting syntax error and bailing out on line 1 for carloM and syntax error and bailing out on line 2 for yazu. I wonder whats wrong Smilie
# 5  
Old 10-27-2011
If Solaris, use nawk

--ahamed
# 6  
Old 10-27-2011
Thank you everyone!. it worked nicely! Smilie

---------- Post updated at 10:47 PM ---------- Previous update was at 08:32 PM ----------

Dear all,

Let say I just want to alter the first row for the second column to a value called, 'type' instead of cf+random numbers.

so basically does the same thing, except, only for the first row, and change the value to 'type', instead of cf10923 for example. How do I this?
# 7  
Old 10-27-2011
'type' is literal text? Do you only want the first row, or the whole file?

Assuming it is and you want the whole thing:
Code:
nawk -F\; 'NR == 1 {sub ($2,"type"); print} NR > 1 {print}' infile.csv

or if you only want row 1:
Code:
nawk -F\; 'NR == 1 {sub ($2,"type"); print}' infile.csv


Last edited by CarloM; 10-27-2011 at 12:02 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace all string matches in file with unique random number

Hello Take this file... Test01 Ref test Version 01 Test02 Ref test Version 02 Test66 Ref test Version 66 Test99 Ref test Version 99 I want to substitute every occurrence of Test{2} with a unique random number, so for example, if I was using sed, substitution would be something... (1 Reply)
Discussion started by: funkman
1 Replies

2. Shell Programming and Scripting

CSV file REPLACE COLUMN if it matches

I have a file cat 1.txt AAAA , BBBB , CCCC , DDDD DFDF , DFDF , DFDF , FDDD AA11 , DFDF , 0000 , UTIO ADSD , WERT, 0000 , JKJL If the 3rd column is not equal to "0000" , then it should replace "0000" with "XXXX" and if its equal to "0000" then print the line as it is. need help. (9 Replies)
Discussion started by: aravindj80
9 Replies

3. Shell Programming and Scripting

To replace the value of the column in a fixed width file

I have a fixed with file with header & trailer length having the same length of the detail record file. The details record length of this file is 24, for Header and Trailer the records will be padded with spaces to match the record length of the file Currently I am adding 3 spaces in header... (14 Replies)
Discussion started by: ginrkf
14 Replies

4. Shell Programming and Scripting

Replace 2nd column in file

I have following entries file abc.txt abc83.out.remote TRUE abc84.out.remote TRUE abc85.out.remote TRUE abc86.out.remote TRUE Please help me, how do i toggle the entries listed in 2nd column based on the search patterns (abcxx) abcxx, i can get... (14 Replies)
Discussion started by: sdosanjh
14 Replies

5. Shell Programming and Scripting

Replace column by random number addition

Here is my problem:- I have a file with pipe separated values. CR|20121021|079|ABC|N|DLS|00038|DLS|04750|1330597704|634234|634|0 CR|20121021|079|ABC|N|DLS|00038|DLS|05118|2071690102|354|351|3 CR|20121021|079|ABC|N|DLS|00038|DLS|05140|960051505|1088|1088|0... (4 Replies)
Discussion started by: Yoda
4 Replies

6. UNIX for Dummies Questions & Answers

how to generate random number as as the first column of a txt file

Dear all, I have a question. I have a txt file say 4000 rows X 1800 Column. I 'd like to creat a new column as the first column which is a column of random numbers (n=4000) thanks a lot! Lin (2 Replies)
Discussion started by: forevertl
2 Replies

7. Shell Programming and Scripting

search a string in a particular column of file and return the line number of the line

Hi All, Can you please guide me to search a string in a particular column of file and return the line number of the line where it was found using awk. As an example : abc.txt 7000,john,2,1,0,1,6 7001,elen,2,2,0,1,7 7002,sami,2,3,0,1,6 7003,mike,1,4,0,2,1 8001,nike,1,5,0,1,8... (3 Replies)
Discussion started by: arunshankar.c
3 Replies

8. Shell Programming and Scripting

splitting a huge line of file into multiple lines with fixed number of columns

Hi, I have a huge file with a single line. But I want to break that line into lines of with each line having five columns. My file is like this: code: "hi","there","how","are","you?","It","was","great","working","with","you.","hope","to","work","you." I want it like this: code:... (1 Reply)
Discussion started by: rajsharma
1 Replies

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

10. UNIX for Dummies Questions & Answers

Sed to replace second number in a random string

I need a sed line that will take STDM111 and change it to STDM161 the STDM will always be constant but the 3 numbers after will be random, I just need it to always replace the middle number with 6 regardless of what the numbers are. (8 Replies)
Discussion started by: glev2005
8 Replies
Login or Register to Ask a Question