Extend the values in a column


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Extend the values in a column
# 1  
Old 10-30-2010
Extend the values in a column

Hello,

i have a file :
Code:
2010-10-28 17:57:42.581
#$!pre-dump!$#00027cv
060400552|060730|0963.000.2|30000261282|137943|176141|theipia_|01232006|1|
060400553|060730|0963.000.2|30000261283|137943|176141|theipia_|01232006|1|
060400554|060730|0963.000.2|30000261284|137943|176141|theipia_|01232006|1|
060400555|060730|0963.000.2|30000261285|137943|176141|theipia_|01232006|1|
060400556|060730|0963.000.2|30000261286|137943|176141|theipia_|01232006|1|
060400557|060730|0963.000.2|30000261287|137943|176141|theipia_|01232006|1|
060400558|060730|0963.000.2|30000261288|137943|176141|theipia_|01232006|1|
060400559|060730|0963.000.2|30000261289|137943|176141|theipia_|01232006|1|

Column 2 value should be extended with 100 and column 3 value should have 9 leading spaces.

The output should be:
Code:
060400552|100060730|"9spaces"0963.000.2|30000261282|137943|176141|theipia_|01232006|1|

How to realize ?

I've tried for column 2:
Code:
awk '{$2=sprintf("100",$2)}2' inputfile > outputfile

But a " 100" is added at the end of each line.
Code:
060400552|060730|0963.000.2|30000261282|137943|176141|theipia_|01232006|1| 100
060400553|060730|0963.000.2|30000261283|137943|176141|theipia_|01232006|1| 100

Thanks in advance.

Last edited by Scott; 10-30-2010 at 08:28 AM.. Reason: Please use code tags
# 2  
Old 10-30-2010
Code:
$ sed "s/|\([^|]*\)|/|100\1|         /" file
2010-10-28 17:57:42.581
#$!pre-dump!$#00027cv
060400552|100060730|         0963.000.2|30000261282|137943|176141|theipia_|01232006|1|
060400553|100060730|         0963.000.2|30000261283|137943|176141|theipia_|01232006|1|
060400554|100060730|         0963.000.2|30000261284|137943|176141|theipia_|01232006|1|
060400555|100060730|         0963.000.2|30000261285|137943|176141|theipia_|01232006|1|
060400556|100060730|         0963.000.2|30000261286|137943|176141|theipia_|01232006|1|
060400557|100060730|         0963.000.2|30000261287|137943|176141|theipia_|01232006|1|
060400558|100060730|         0963.000.2|30000261288|137943|176141|theipia_|01232006|1|
060400559|100060730|         0963.000.2|30000261289|137943|176141|theipia_|01232006|1|


Last edited by Scott; 10-30-2010 at 08:37 AM.. Reason: Removed awk - was rubbish!
This User Gave Thanks to Scott For This Post:
# 3  
Old 10-30-2010
Thanks, i've done :

Quote:
Originally Posted by scottn
Code:
$ sed "s/|\([^|]*\)|/|100\1|         /" file
2010-10-28 17:57:42.581
#$!pre-dump!$#00027cv
060400552|100060730|         0963.000.2|30000261282|137943|176141|theipia_|01232006|1|
060400553|100060730|         0963.000.2|30000261283|137943|176141|theipia_|01232006|1|
060400554|100060730|         0963.000.2|30000261284|137943|176141|theipia_|01232006|1|
060400555|100060730|         0963.000.2|30000261285|137943|176141|theipia_|01232006|1|
060400556|100060730|         0963.000.2|30000261286|137943|176141|theipia_|01232006|1|
060400557|100060730|         0963.000.2|30000261287|137943|176141|theipia_|01232006|1|
060400558|100060730|         0963.000.2|30000261288|137943|176141|theipia_|01232006|1|
060400559|100060730|         0963.000.2|30000261289|137943|176141|theipia_|01232006|1|

sed "s/|\([^|]*\)|/|100\1| /" file > newfile

Your solution works like a charme and save a lot of time.
# 4  
Old 10-30-2010
Code:
awk  'BEGIN{FS=OFS="|"}NR>2{$2="100" $2; $3="         " $3}1' infile

This User Gave Thanks to rdcwayx For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script to append suffix to column when column has duplicated values

Please help me to get required output for both scenario 1 and scenario 2 and need separate code for both scenario 1 and scenario 2 Scenario 1 i need to do below changes only when column1 is CR and column3 has duplicates rows/values. This inputfile can contain 100 of this duplicated rows of... (1 Reply)
Discussion started by: as7951
1 Replies

2. Shell Programming and Scripting

Bring values in the second column into single line (comma sep) for uniq value in the first column

I want to bring values in the second column into single line for uniq value in the first column. My input jvm01, Web 2.0 Feature Pack Library jvm01, IBM WebSphere JAX-RS jvm01, Custom01 Shared Library jvm02, Web 2.0 Feature Pack Library jvm02, IBM WebSphere JAX-RS jvm03, Web 2.0 Feature... (10 Replies)
Discussion started by: kchinnam
10 Replies

3. UNIX for Dummies Questions & Answers

Match sum of values in each column with the corresponding column value present in trailer record

Hi All, I have a requirement where I need to find sum of values from column D through O present in a CSV file and check whether the sum of each Individual column matches with the value present for that corresponding column present in the trailer record. For example, let's assume for column D... (9 Replies)
Discussion started by: tpk
9 Replies

4. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

5. Shell Programming and Scripting

Converting odd values to even values(or vice-versa) located in a column

Hello All, I have a below data in a .csv file where all rows where col1 is A, col2 is odd numbers, similarly even numbers for all rows where col1 is B. Note that my data has some other columns(not shown here) too (around 100) after col2. Tool,Data A,1 A,3 A,5 .... so on B,2 B,4 .... ... (4 Replies)
Discussion started by: ks_reddy
4 Replies

6. UNIX for Dummies Questions & Answers

shift values in one column as header for values in another column

Hi Gurus, I have a tab separated text file with two columns. I would like to make the first column values as headings for the second column values. Ex. >value1 subjects >value2 priorities >value3 requirements ...etc and I want to have a file >value1 subjects >value2 priorities... (4 Replies)
Discussion started by: Unilearn
4 Replies

7. Shell Programming and Scripting

Cat Values from Several files if it meets criteria for column values

I have results from some statistical analyses. The format of the results are as given below: I want to select lines that have a p-value (last column) less than 0.05 from all the results files (*.results) and cat to a new results file. It would be very nice if a new column is added that tells... (2 Replies)
Discussion started by: genehunter
2 Replies

8. Shell Programming and Scripting

print unique values of a column and sum up the corresponding values in next column

Hi All, I have a file which is having 3 columns as (string string integer) a b 1 x y 2 p k 5 y y 4 ..... ..... Question: I want get the unique value of column 2 in a sorted way(on column 2) and the sum of the 3rd column of the corresponding rows. e.g the above file should return the... (6 Replies)
Discussion started by: amigarus
6 Replies

9. Shell Programming and Scripting

How to pick values from column based on key values by usin AWK

Dear Guyz:) I have 2 different input files like this. I would like to pick the values or letters from the inputfile2 based on inputfile1 keys (A,F,N,X,Z). I have done similar task by using awk but in that case the inputfiles are similar like in inputfile2 (all keys in 1st column and values in... (16 Replies)
Discussion started by: repinementer
16 Replies

10. Shell Programming and Scripting

How to check Null values in a file column by column if columns are Not NULLs

Hi All, I have a table with 10 columns. Some columns(2nd,4th,5th,7th,8th and 10th) are Not Null columns. I'll get a tab-delimited file and want to check col by col and generate seperate error code for each col eg:102 if 2nd col value is NULL and 104 if 4th col value is NULL so on... I am a... (7 Replies)
Discussion started by: Mandab
7 Replies
Login or Register to Ask a Question