Calculate the length and add some string infront


 
Thread Tools Search this Thread
Operating Systems Solaris Calculate the length and add some string infront
# 1  
Old 02-13-2008
Question Calculate the length and add some string infront

Hi all,

I have file having 4 coulmn like

9246309198,406655682,400009246309198
9246309198,9246309198,400009246309198
9246309198,9246309198,400009246309198
9246309198,9246309198,400009246309198
9246309198,406655682,400009246309198
9246309198,9246309198,400009246309198

I want to valid the 2nd column if the length of the 2nd column is 9 digit i want add astring X beginning of the 2nd column.Which will give me the Output like.


9246309198,X406655682,400009246309198
9246309198,9246309198,400009246309198
9246309198,9246309198,400009246309198
9246309198,9246309198,400009246309198
9246309198,X406655682,400009246309198
9246309198,9246309198,400009246309198

Dears Is it possible?.
# 2  
Old 02-14-2008
hi Salathi,

hope this will work for u,

awk -F, '{ if (length($2) == 9) {print $1",""X"$2","$3;} else { print $0}}' filename
# 3  
Old 02-14-2008
Quote:
Originally Posted by salaathi
Hi all,

I have file having 4 coulmn like

9246309198,406655682,400009246309198
9246309198,9246309198,400009246309198
9246309198,9246309198,400009246309198
9246309198,9246309198,400009246309198
9246309198,406655682,400009246309198
9246309198,9246309198,400009246309198

I want to valid the 2nd column if the length of the 2nd column is 9 digit i want add astring X beginning of the 2nd column.Which will give me the Output like.


9246309198,X406655682,400009246309198
9246309198,9246309198,400009246309198
9246309198,9246309198,400009246309198
9246309198,9246309198,400009246309198
9246309198,X406655682,400009246309198
9246309198,9246309198,400009246309198

Dears Is it possible?.
Code:
sed -e "s/^\([^,]*,\)\([0-9]\{9\}\),/\1X\2,/g" input.txt

or
Code:
sed -e "s/^\([^,]*,\)\([^,]\{9\}\),/\1X\2,/g"

# 4  
Old 02-14-2008
MySQL

Dears,
Both the codes worked for me.

Thanks a ton.
Vino,
I am able to understand the awk syntax.
Can u please explain the sed code.
# 5  
Old 02-14-2008
Quote:
Originally Posted by salaathi
Dears,
Both the codes worked for me.

Thanks a ton.
Vino,
I am able to understand the awk syntax.
Can u please explain the sed code.
sed -e "s/^\([^,]*,\)\([^,]\{9\}\),/\1X\2,/g"

^ - start at the beginning of each line
^\([^,]*,\) - collect that part of the string which starts from the beginning upto the place where you encounter the first comma and place it in buffer1

\([^,]\{9\}\) - Then collect any non-comma set of characters who length is 9 and then followed by a comma and then place it in buffer2

\1X\2, - dump the contents of buffer1 followed by a X followed by the contents of buffer2 followed by a comma.
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add string based on character length

Good day, I am a newbie here and thanks for accepting me I have a task to modify input data where my input data looks like 123|34567|CHINE 1|23|INDIA 34512|21|USA 104|901|INDIASee that my input has two columns with different character length but max length is 5 and minimum length is 0 which... (1 Reply)
Discussion started by: fastlearner
1 Replies

2. Shell Programming and Scripting

Calculate the length of the each variable and matches to the desired value

My script is currently doing two function and I want to add one more as pre the question. echo -en "Enter the logmsg=" read logmsg logmsg1=${logmsg%%|*}; logmsg_len2=$(echo ${logmsg} | awk '{print $2}'); logmsg_suffix="|"; Char() { #echo $logmsg1 echo $logmsg1 | tr ',' '\n' | awk... (4 Replies)
Discussion started by: rohit22hamirpur
4 Replies

3. Shell Programming and Scripting

How can use the perl or other command line to calculate the length of the character?

For example, if I have the file whose content are: >HWI-EAS382_30FC7AAXX:7:1:927:1368 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA >HWI-EAS382_30FC7AAXX:7:1:924:1373 ACGAACTTTAAAGCACCTCTTGGCTCGTATGCCGTC I want my output calculate the total length of nucleotide. So my output should look like this:... (1 Reply)
Discussion started by: patrick chia
1 Replies

4. UNIX for Dummies Questions & Answers

Read a string with leading spaces and find the length of the string

HI In my script, i am reading the input from the user and want to find the length of the string. The input may contain leading spaces. Right now, when leading spaces are there, they are not counted. Kindly help me My script is like below. I am using the ksh. #!/usr/bin/ksh echo... (2 Replies)
Discussion started by: dayamatrix
2 Replies

5. Shell Programming and Scripting

read string, check string length and cut

Hello All, Plz help me with: I have a csv file with data separated by ',' and optionally enclosed by "". I want to check each of these values to see if they exceed the specified string length, and if they do I want to cut just that value to the max length allowed and keep the csv format as it... (9 Replies)
Discussion started by: ozzy80
9 Replies

6. Shell Programming and Scripting

sed problem - replacement string should be same length as matching string.

Hi guys, I hope you can help me with my problem. I have a text file that contains lines like this: 78 ANGELO -809.05 79 ANGELO2 -5,000.06 I need to find all occurences of amounts that are negative and replace them with x's 78 ANGELO xxxxxxx 79... (4 Replies)
Discussion started by: amangeles
4 Replies
Login or Register to Ask a Question