Splitting the numeric vs alpha values in a column to distinct columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Splitting the numeric vs alpha values in a column to distinct columns
# 1  
Old 05-28-2014
Splitting the numeric vs alpha values in a column to distinct columns

How could i take an input file and split the numeric values from the alpha values (123 vs abc) to distinc columns, and if the source is blank to keep it blank (null) in both of the new columns:


So if the source file had a column like:

Value:
Code:
|1   |
|2.3|
|     |
|No|

I would create a new appended file containing a column with the numeric:
Numeric:
Code:
|1   |
|2.3|
|     |
|     |

And a column with the abc:
Code:
|     |
|     |
|     |
|No|

Thanks,
DL

Moderator's Comments:
Mod Comment Use code tags!
# 2  
Old 05-28-2014
Code:
awk -F'|' '{if ($2 ~ /[^0-9. ]/) {$(NF+1)=$2;$2=" "}}NF++' OFS='|' myFile

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 05-28-2014
vgresh thank you. i'm goign to see if i can use your code. The file in question actually has 1.3million rows (tab delimited). the source column is number 35, and I need to append the 123 and abc column as two new columns to the end of the file.

---------- Post updated at 02:27 PM ---------- Previous update was at 02:24 PM ----------

I should also add it is possible that something that would need to be parsed to the abc column is: < 3.2

---------- Post updated at 02:39 PM ---------- Previous update was at 02:27 PM ----------

Quote:
Originally Posted by vgersh99
Code:
awk -F'|' '{if ($2 ~ /[^0-9. ]/) {$(NF+1)=$2;$2=" "}}NF++' OFS='|' myFile

vgresh! thank you this is almost there.

currently only the abc values are moved to a new column. I also need the 123 values to move to their own distinct column.

in addition I need to copy these values to new columns and retain them in the source column.

cheers! ty!
# 4  
Old 05-28-2014
please provide a small representative sample of the ACTUAL data and the desired output. A 10 record/line representative sample should be enough.
Please either code tags OR attach a file to a thread.
This User Gave Thanks to vgersh99 For This Post:
# 5  
Old 05-28-2014
hi vgresh,

Here is a representative sample attached (input worksheet).
Desired output is on the output worksheet.
# 6  
Old 05-28-2014
assuming fields are pipe separated - if not change -F and OFS= to the appropriate characters:
Code:
awk -F'|' '{NF+=2;;$NF=$(NF-1)=""}{($1 ~ /[^0-9. ]/)?$(NF-1)=$1:$(NF)=$1}1' OFS='|' myFile

or better yet - dealing with empty input fileds AND specifying what field number (f) you want to check:
Code:
awk -F'|' '{NF+=(!NF)?3:2;$NF=$(NF-1)=""}{($f ~ /[^0-9. ]/)?$(NF-1)=$f:$(NF)=$f}1' f=1 OFS='|' myFile


Last edited by vgersh99; 05-28-2014 at 06:41 PM..
# 7  
Old 05-28-2014
Input file myFile, TAB separated fields, work with field#1, append two columns to each line:
Code:
awk 'BEGIN{FS=OFS="\t"} {spc=$1; gsub(/./," ",spc); if ($field~/[^0-9.]/) {print $0,$field,spc} else {print $0,spc,$field}}' field=1 myFile

It tries to keep the old field width. Otherwise it is just
Code:
awk 'BEGIN{FS=OFS="\t"; spc=""} {if ($field~/[^0-9.]/) {print $0,$field,spc} else {print $0,spc,$field}}'  field=1 myFile


Last edited by MadeInGermany; 05-28-2014 at 07:09 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

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. UNIX for Advanced & Expert Users

Sort by second column numeric values

From googling and reading man pages I figured out this sorts the first column by numeric values. sort -g -k 1,1 Why does the -n option not work? The man pages were a bit confusing. And what if I want to sort the second column numerically? I haven't been able to figure that out. The file... (7 Replies)
Discussion started by: cokedude
7 Replies

3. Shell Programming and Scripting

Add the values in second and third columns with group by on first column.

Hi All, I have a pipe seperated file. I need to add the values in second and third columns with group by on first column. MYFILE_28012012_1115|47|173.90 MYFILE_28012012_1115|4|0.00 MYFILE_28012012_1115|6|22.20 MYFILE_28012012_1116|47|173.90 MYFILE_28012012_1116|4|0.00... (3 Replies)
Discussion started by: angshuman
3 Replies

4. Shell Programming and Scripting

Average of columns with values of other column with same name

I have a lot of input files that have the following form: Sample Cq Sample Cq Sample Cq Sample Cq Sample Cq 1WBIN 23.45 1WBIN 23.45 1CVSIN 23.96 1CVSIN 23.14 S1 31.37 1WBIN 23.53 1WBIN 23.53 1CVSIN 23.81 1CVSIN 23.24 S1 31.49 1WBIN 24.55 1WBIN 24.55 1CVSIN 23.86 1CVSIN 23.24 S1 31.74 ... (3 Replies)
Discussion started by: isildur1234
3 Replies

5. UNIX for Dummies Questions & Answers

count number of distinct values in each column with awk

Hi ! input: A|B|C|D A|F|C|E A|B|I|C A|T|I|B As the title of the thread says, I would need to get: 1|3|2|4 I tried different variants of this command, but I don't manage to obtain what I need: gawk 'BEGIN{FS=OFS="|"}{for(i=1; i<=NF; i++) a++} END {for (b in a) print b}' input ... (2 Replies)
Discussion started by: beca123456
2 Replies

6. Shell Programming and Scripting

Splitting the data in a column into several columns

Hi, I have the following input file 32895901-d17f-414c-ac93-3e7e0f5ec240 AND @GDF_INPUT 73b129e1-1fa9-4c0d-b95b-4682e5389612 AUS @GDF_INPUT 40f82e88-d1ff-4ce2-9b8e-d827ddb39447 BEL @GDF_INPUT 36e9c3f1-042a-43a4-a80e-4a3bc2513d01 BGR @GDF_INPUT I want to split column 3 into two columns:... (1 Reply)
Discussion started by: ramky79
1 Replies

7. Shell Programming and Scripting

Splitting file based on column values

Hi all, I have a file (say file.txt) which contains comma-separated rows. Each row has seven columns. Only column 4 or 5 (not both) can have empty values like "" in each line. Sample lines So, now i want all the rows that have column 4 as "" go in file1.txt and all the rows that have column... (8 Replies)
Discussion started by: jakSun8
8 Replies

8. Shell Programming and Scripting

splitting single column values into text and number component

Hey guys, I have a column that consists of string and integer values without a distinctive deliminator, looking like this... 7ASA 14LAL 245FOO 656MOM 87577DAD ... I want to split the column into two columns, one containing the numbers and one containing the text part. edit: numbers... (3 Replies)
Discussion started by: origamisven
3 Replies

9. Shell Programming and Scripting

have to retrieve the distinct values (not duplicate) from 2nd column and display

I have a text file names test2 with 3 columns as below . We have to retrieve the distinct values (not duplicate) from 2nd column and display. I have used the below command but giving some error. NS3303 NS CRAFT LTD NS3303 NS CHIRON VACCINES LTD NS3303 NS ALLIED MEDICARE LTD NS3303 NS... (16 Replies)
Discussion started by: shirdi
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