Visit Our UNIX and Linux User Community


Insert Columns before the last Column based on the Count of Delimiters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert Columns before the last Column based on the Count of Delimiters
# 1  
Old 08-22-2014
Insert Columns before the last Column based on the Count of Delimiters

Hi,

I have a requirement where in I need to insert delimiters before the last column of the total delimiters is less than a specified number.

Say if the delimiters is less than 139, I need to insert 2 columns ( with blanks) before the last field
Code:
awk -F '' '{ if (NF-1 < 139)} END { "Insert 2 columns before the last Field" }' File.txt


Last edited by bartus11; 08-22-2014 at 01:43 PM.. Reason: Please use [code][/code] tags.
# 2  
Old 08-22-2014
Try (untested):
Code:
awk -F 'NF < 140 {NF+=2; $NF=$(NF-2);$(NF-2)=""}' file

This would add two columns even if there's just e.g. three of them. Is that what you want?
# 3  
Old 08-22-2014
Hi,

Thanks for the reply..

I have tried that but it isn't working.

My requirement is certain records in my file have 139 columns and few others 137. For all the records which have 137 columns , I need to add 2 columns before the last column so that the total count becomes 139
# 4  
Old 08-22-2014
Try:
Code:
awk 'NF<c{n=NF; $c=$n; $n=x}1' c=139 FS= OFS= file

# 5  
Old 08-22-2014
Hi,
I have executed the command on the file, but when i count the delimiters i still get 138 and 139.Earlier it ised to be 137 and 139.

Ideally I should be getting one sinle value of the delimiters :139.

Also could yo ulet me know the fuctionality of the commad you used.

Many Thanks
# 6  
Old 08-22-2014
c (139) is the number of columns. To get 139 delimiters use c=140 instead of c=139..

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux shell script to insert new lines based on delimiter count

The input file is a .dat file which is delimited by null (^@ in Linux). On a windows PC it looks something like this (numbers are masked with 1). https://i.imgur.com/nta2Gqp.jpg The entire file is in one row but it has multiple records - each record contains 80 fields i.e. there are 81 counts... (9 Replies)
Discussion started by: digitalnirvana
9 Replies

2. Shell Programming and Scripting

Convert rows to columns based on key and count

Team, I am having requirement to convert rows to columns Input is: key ,count, id1, pulse1, id2, pulse2 ,id3, pulse3 12, 2 , 14 , 56 , 15, 65 13, 3, 12, 32, 14, 23, 18, 54 22, 1 , 32, 42 Expected Out put: key, id,pulse 12, 14, 56 12, 15, 65 13 ,12, 32 13, 14 ,23 13, 18 ,54 22 ,32,... (3 Replies)
Discussion started by: syam1406
3 Replies

3. Shell Programming and Scripting

Insert value of column based on file name matching

At the top of the XYZ file, I need to insert the ABC data value of column 2 only when ABC column 1 matches the prefix XYZ file name (not the ".txt"). Is there an awk solution for this? ABC Data 0101 0.54 0102 0.48 0103 1.63 XYZ File Name 0101.txt 0102.txt 0103.txt ... (7 Replies)
Discussion started by: ncwxpanther
7 Replies

4. Shell Programming and Scripting

Insert space in specific column among many columns

Hello, I have some problem in inserting the space for the pairs of columns. I have the input file : I used this code below in replacing it using space in specific column (replace space in each two columns) sed -e "s/,/ /2" -e "s/,/ /3" inputfile Output showed : However, I have many... (3 Replies)
Discussion started by: awil
3 Replies

5. Shell Programming and Scripting

count the unique records based on certain columns

Hi everyone, I have a file result.txt with records as following and another file mirna.txt with a list of miRNAs e.g. miR22, miR123, miR13 etc. Gene Transcript miRNA Gar Nm_111233 miR22 Gar Nm_123440 miR22 Gar Nm_129939 miR22 Hel Nm_233900 miR13 Hel ... (6 Replies)
Discussion started by: miclow
6 Replies

6. Shell Programming and Scripting

Count Columns and If less add new column

Hi All, My Input.txt should have always 4 columns in some cases i may not get all the 4columns data. My requirement is if columns as not equal to 4 then append new column delimiter. Input.txt A,1,2 B,1,2,3 C,1 Desired output should be in below format Output.txt A,1,2,... (3 Replies)
Discussion started by: kmsekhar
3 Replies

7. UNIX for Dummies Questions & Answers

How to insert alternative columns and sort text from first column to second?

Hi Everybody, I am just new to UNIX as well as to this forum. I have a text file with 10,000 coloumns and each coloumn contains values separated by space. I want to separate them into new coloumns..the file is something like this as ad af 1 A as ad af 1 D ... ... 1 and A are in one... (7 Replies)
Discussion started by: Unilearn
7 Replies

8. Shell Programming and Scripting

Insert new line based on numerical number of column

My input file: Class Number Position Range 1 Initial 50 1 Initial 50 2 Terminal 150 2 Terminal 20 2 Single 10 3 Single 20 4 Double 50 5 Initial 50 5 Initial 60 Class Number... (11 Replies)
Discussion started by: patrick87
11 Replies

9. Shell Programming and Scripting

insert leading zeroes based on the character count

Hi, I need add leading zeroes to a field in a file based on the character count. The field can be of 1 character to 6 character length. I need to make the field 14bytes. eg: 8351,20,1 8351,234,6 8351,2,0 8351,1234,2 8351,123456,1 8351,12345,2 This should become. ... (3 Replies)
Discussion started by: gpaulose
3 Replies

10. Shell Programming and Scripting

Count first column on the basis of two other columns

Hello, I have a file ================= 12 SRV1 GRP1 19 SRV1 GRP1 19 SRV1 GRP2 3 SRV1 GRP1 3 SRV1 GRP2 30 SRV1 GRP2 7 SRV1 GRP1 8 SRV1 GRP3 =========== I want output like =============== 41 SRV1 GRP1 52 SRV1 GRP2 8 SRV1 GRP3 (1 Reply)
Discussion started by: kaustubh137
1 Replies

Featured Tech Videos