Increment Column having Alphanumeric value in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Increment Column having Alphanumeric value in file
# 1  
Old 05-04-2015
Linux Increment Column having Alphanumeric value in file

I want to replace a column(first and last) having an alphanumeric value in a file.
Requirement :
1)All values in a Column must be unique and contain an incremented pattern “HCTV0096” for first column and “cafefeca0090” for last column
2)for uniquely identifying each value in column Numeric part must be incremented by 1 for both column

Output must be :
Code:
HDTV0092|Subcenre B| Sa_WSDSD |bocvdf| caddfdfefeca0090
HDTV0093|Subcenre B| Sa_WSDSD |bocvdf| caddfdfefeca0091
HDTV0094|Subcenre B| Sa_WSDSD |bocvdf| caddfdfefeca0092
HDTV0095|Subcenre B| Sa_WSDSD |bocvdf| caddfdfefeca0093
:
:

I have tried, and got below only for first column
Code:
awk -F '|' '{print "HDTV"$1+1,$0}' file1

but it gives :
Code:
HDTV1 HDTV0092|Subcenre B| Sa_WSDSD |bocvis|caddfdfefeca0090
HDTV1 HDTV0093|Subcenre B| Sa_WSDSD |bocvdf| caddfdfefeca0091
HDTV1 HDTV0094|Subcenre B| Sa_WSDSD |bocvdf| caddfdfefeca0092
HDTV1 HDTV0095|Subcenre B| Sa_WSDSD |bocvdf| caddfdfefeca0093

Kindly help me with this..Smilie
# 2  
Old 05-04-2015
Could you provide a sample of your input file?
# 3  
Old 05-04-2015
Neither of your samples complies to your specification. What quality of solutions do you expect?
# 4  
Old 05-05-2015
User will provide two input parameters
1.UID
2.location_ID

and if parameters are correct then i have to ask for No. of lines i.e no of records to be generated within file defined as below format(----- | Subcenre B| Sa_WSDSD |bocvdf |------) will remain as it is for all records.
Code:
Userid | <Default> | <Default>| <Default> | location_ID
HDTV0092|Subcenre B| Sa_WSDSD |bocvdf| caddfdfefeca0090
HDTV0093|Subcenre B| Sa_WSDSD |bocvdf| caddfdfefeca0091
HDTV0094|Subcenre B| Sa_WSDSD |bocvdf| caddfdfefeca0092
HDTV0095|Subcenre B| Sa_WSDSD |bocvdf| caddfdfefeca0093

i am facing issue to generate incremental(Unique) values for first(HDTV000(+1)) and last Alphanumeric columns.
# 5  
Old 05-05-2015
Still not sure what you require, maybe this will give you some ideas:
Code:
awk -v U=96 -v L=91 -v n=4 'BEGIN{for(i=1; i<=n; i++) printf "HDTV%04d|Subcenre B| Sa_WSDSD |bocvdf| caddfdfefeca%04d\n", U++, L++}'

Output:
Code:
HDTV0096|Subcenre B| Sa_WSDSD |bocvdf| caddfdfefeca0091
HDTV0097|Subcenre B| Sa_WSDSD |bocvdf| caddfdfefeca0092
HDTV0098|Subcenre B| Sa_WSDSD |bocvdf| caddfdfefeca0093
HDTV0099|Subcenre B| Sa_WSDSD |bocvdf| caddfdfefeca0094

This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 05-05-2015
Thanks Scrutinizer,this is what m looking for.

I have done some changes as required in script, but awk give me errors while calling parameters.I have tried with " ' ' " but its not working...any suggestion?
Code:
awk -v U=0 -v L=0 -v n=$number 'BEGIN{for(i=1; i<=n; i++) printf ''"$u_id"'%04d|'"$u_loc"'|'"$u_pkg"'|bocvdf|'"$u_cmmac"'%04d\n', U++, L++}' >> dataspecific_$(date '+%d%m%Y%H%M')


ERRORS :
Code:
awk: BEGIN{for(i=1; i<=n; i++) printf AAAAAA%04d|LLLLLL|OKLN SSDD|bocvdf|BBBBBB%04d\n, U++, L++}
awk:                                                    ^ syntax error
awk: BEGIN{for(i=1; i<=n; i++) printf AAAAAA%04d|LLLLLL|OKLN SSDD|bocvdf|BBBBBB%04d\n, U++, L++}
awk:                                                                               ^ backslash not last character on line
+ awk -v U=0 -v L=0 -v n=10 'BEGIN{for(i=1; i<=n; i++) printf "$u_id"%04d'
+ '"$u_pkg"'
+ docsis
./case.sh: line 11: "$u_pkg": command not found
./case.sh: line 11: bocvdf: command not found
+ '"$u_loc"'
./case.sh: line 11: "$u_loc": command not found
+ '"$u_cmmac"%04dn, U++, L++}'
date '+%d%m%Y%H%M')
awk: cmd. line:1: BEGIN{for(i=1; i<=n; i++) printf "$u_id"%04d
awk: cmd. line:1:                                             ^ unexpected newline or end of string


Last edited by ketanraut; 05-05-2015 at 09:37 AM.. Reason: Updating Information..
# 7  
Old 05-05-2015
Try something like:

Code:
number=4 u_uid=HDTV u_loc="Subcenre B" u_pkg=Sa_WSDSD u_cmmac=caddfdfefec

awk -v uuid="$u_uid" -v uloc="$u_loc" -v upkg="$u_pkg" -v ucmmac="$u_cmmac" -v U=0 -v L=0 -v n=$number '
  BEGIN{
    for(i=1; i<=n; i++) 
      printf "%s%04d|%s|%s|bocvdf|%s%04d\n", uuid, U++, uloc, upkg, ucmmac, L++
  }
'

Output:
Code:
HDTV0000|Subcenre B|Sa_WSDSD|bocvdf|caddfdfefeca0000
HDTV0001|Subcenre B|Sa_WSDSD|bocvdf|caddfdfefeca0001
HDTV0002|Subcenre B|Sa_WSDSD|bocvdf|caddfdfefeca0002
HDTV0003|Subcenre B|Sa_WSDSD|bocvdf|caddfdfefeca0003

or maybe me just use shell, for example in bash:

Code:
number=4 u_uid=HDTV u_loc="Subcenre B" u_pkg=Sa_WSDSD u_cmmac=caddfdfefec L=0 U=0
for ((i=1; i<=number; i++))
do
   printf "%s%04d|%s|%s|bocvdf|%s%04d\n" "$u_uid" $((U++)) "$u_loc" "$u_pkg" "$u_cmmac" $((L++))
done

Output:
Code:
HDTV0000|Subcenre B|Sa_WSDSD|bocvdf|caddfdfefeca0000
HDTV0001|Subcenre B|Sa_WSDSD|bocvdf|caddfdfefeca0001
HDTV0002|Subcenre B|Sa_WSDSD|bocvdf|caddfdfefeca0002
HDTV0003|Subcenre B|Sa_WSDSD|bocvdf|caddfdfefeca0003


Last edited by Scrutinizer; 05-05-2015 at 03:35 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Moving alphanumeric files according to the digit in file name

This is the content of my directory c_g_se1_gb.ph c_g_se1_gb.ph_pl_s.t c_g_se1_gb.ph_pl_tr.t c_g_se3_gb.ph c_g_se3_gb.ph_pl_s.t c_g_se3_gb.ph_pl_tr.t c_g_se2_gb.ph c_g_se2_gb.ph_pl_s.t c_g_se2_gb.ph_pl_tr.t c_g_se4_gb-1.ph c_g_se4_gb-1.ph_pl_s.t c_g_se4_gb-1.ph_pl_tr.t... (9 Replies)
Discussion started by: sammy777888
9 Replies

2. Shell Programming and Scripting

Check increment values in column

Gents, I have a file where i would like to check the constant increment by 2 in column 2. 5450 1000 5450 1002 5450 1004 5450 1006 5465 1000 5465 1002 5465 1006 5465 1008 5550 1002 5550 1004 5550 1006 5550 1008 6830 1000 6830 1002 6830 1008 6830 1010 (6 Replies)
Discussion started by: jiam912
6 Replies

3. Shell Programming and Scripting

Create new file with increment column based on conditions

Hello, Using bash script, i need to process the following file: 887,86,,2013-11-06,1,10030,5,2,0,200,, 887,86,,2013-11-05,1,10030,5,2,0,199,, 887,138,,2013-11-06,1,10031,6,2,0,1610612736,, 887,164,,2013-11-06,1,10000,0,2,0,36000,, and to create a new file such as the below ... (2 Replies)
Discussion started by: JonhyDeep
2 Replies

4. UNIX for Dummies Questions & Answers

Want to sort a file which contains alphanumeric strings

I want to sort a file which contains alphanumeric string. bash-3.00$ cat abc mtng1so mtng2so mtng11so mtng9so mtng23so mtng7so hstg2so hstg9so hstg1so hstg11so hstg13so bash-3.00$ Want output like this, using one liner. hstg1so (1 Reply)
Discussion started by: Raza Ali
1 Replies

5. Shell Programming and Scripting

Increment existing column in file

Hi All, I have a file with 3 millions records in which 3rd column is same throughout say its value is 0 throughout.for example: Col1 Col2 Col3 Col4 A 1 0 5 B 2 0 6 C 3 0 7 D 4 0 9 I want my output as : Col1 Col2 Col3 Col4 A 1 ... (4 Replies)
Discussion started by: Pinky456
4 Replies

6. Shell Programming and Scripting

Increment Numbers in File

Hello, I have a text file withe some records 20121031|5 20121030|3 20121029|1 20121028|4 20121027|6 I want to search for a patten with '20121030' and then increment the second part of the delimiter i.e. 3 by 1 to make it 4 to look like 20121031|5 20121030|4 20121029|1 20121028|4... (7 Replies)
Discussion started by: pparthiv
7 Replies

7. Shell Programming and Scripting

Searching Alphanumeric Character From a File

Hi, In a error log file, the error code of a particular error contains both Alphabet and Numbers. My problem statement is to find the error codes from a particular log. That means, I need to search a word, which contains both alphabet and number. Please help me. Below is two examples of error... (1 Reply)
Discussion started by: snehasish_jana
1 Replies

8. Shell Programming and Scripting

File existence and increment

count=0; while read line; do ] && let count=count+1; done < file_name.txt echo echo "$count of 10 files found " echo The scenario is a follows : I have a file which contains a list of filenames present in particular directory . I am checking fo the existence of the file and... (5 Replies)
Discussion started by: ultimatix
5 Replies

9. Shell Programming and Scripting

Check file and increment

My scripts excepts 4 files ABCD_01 ABCD_02 ABCD_03 ABCD_04 I want to check for these files , and increment counter one by one . at the end i would like to echo as 4 of 4 expected instances of file found . I tried something like thsi $counter =1 if counter=counter+1 i need... (5 Replies)
Discussion started by: ultimatix
5 Replies

10. Shell Programming and Scripting

Increment a column using awk

Hi, I have a sample file like below: 213~!0~!Feb 16 2009 4:57:29:833PM~!0 212~!0~!Feb 7 2009 5:29:57:760PM~!0 211~!0~!Feb 4 2009 5:51:40:863PM~!0 209~!0~!Dec 17 2008 3:19:05:043PM~!0 206~!0~!Dec 4 2007 4:01:02:850PM~!0 "~!" is the field seperator. I need to replace the... (5 Replies)
Discussion started by: h_banka
5 Replies
Login or Register to Ask a Question