awk substr delimeter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk substr delimeter
# 1  
Old 02-17-2016
awk substr delimeter

Hi All,

I have an awk command that uses the substr function - At the moment I know the length of the values so can use the example below i.e substr(i,0,1)

However in future these lengths may change so wondered if you can use a delimiter within a substr? Like in bash you could use cut -d ';', is there a similar function in awk?

Code:
awk 'BEGIN{FS=OFS=","}
NR==1{print;next}
{a[$4";"$7";"$8";"$13]+=$9}
END{for(i in a)print $1, $2, $3, substr(i,0,1), $5, $6, substr(i,3,10), substr(i,14,10), a[i], $10, $11, $12}' file.txt

IN the above example

Code:
 
 i=2;2015-12-01;2016-12-01;Y1

If for example the first value (2) changed to 0002 then substr(i,0,1) would not pick up the entire value.

Thanks in advance for your help.
# 2  
Old 02-17-2016
Why don't you use the split function?
# 3  
Old 02-17-2016
Hi Rudi,

I am not aware of the Split function - Can you provide an example please?
# 4  
Old 02-17-2016
man awk:
Quote:
split(s,A,r) split(s,A)
String s is split into fields by regular expression r and the fields are loaded into array A. The number of fields is returned. See section 11 below
for more detail. If r is omitted, FS is used.
---------- Post updated at 15:07 ---------- Previous update was at 15:07 ----------

Code:
awk 'BEGIN {n = split ("i=2;2015-12-01;2016-12-01;Y1", T, ";"); for (i=1; i<=n; i++) print "T[" i "]: ", T[i]}'
T[1]:  i=2
T[2]:  2015-12-01
T[3]:  2016-12-01
T[4]:  Y1

# 5  
Old 02-17-2016
Rudi,

I have the below code;

Code:
awk 'BEGIN{FS=OFS=","}
NR==1{print;next}
{a[$4";"$7";"$8";"$13]+=$9}
END{for(i in a) print $1, $2, $3, split(i,b,";") b[1]}' BUCKETED.csv > COL_TOTAL.csv

However when I run, the output is as below

Code:
C,ABC,ABCD,43
C,ABC,ABCD,43
C,ABC,ABCD,43
C,ABC,ABCD,43
C,ABC,ABCD,43
C,ABC,ABCD,44

The code seems to be adding 4 at the beginning of all the values.

I tried moving split to the start

Code:
awk 'BEGIN{FS=OFS=","}
NR==1{print;next}
{a[$4";"$7";"$8";"$13]+=$9}
END{for(i in a) split(i,b,";"); print $1, $2, $3, b[1]}' BUCKETED.csv > COL_TOTAL.csv

But this only output one record and with the wrong value again

Code:
C,ABC,ABCD,4

# 6  
Old 02-17-2016
Quote:
Originally Posted by Ads89
.
.
.
The code seems to be adding 4 at the beginning of all the values.
.
.
.
The 4 is the element count from the conversion; see above:
Quote:
The number of fields is returned
Can't you see the error in
Quote:
.
.
.
Code:
{for(i in a) split(i,b,";"); print $1, $2, $3, b[1]}

.
.
.
?

---------- Post updated at 16:54 ---------- Previous update was at 16:52 ----------

Enclose split and print in curly braces:
Code:
{for(i in a) {split(i,b,";"); print $1, $2, $3, b[1]}}

This User Gave Thanks to RudiC For This Post:
# 7  
Old 02-17-2016
Apologies - new to awk and trying to get my head around the syntax. This solution works great, thanks again for your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk with tab delimeter

Hi Team below test file contains tab delimeter file and i am excepting the number of files 3. File : test.txt a b c awk -vFPAT='\t' -vOFS="\t" -v a="0" -v b="10" ' NR>a {if (NF != b ) print NR"@"NF }' test.txt current output is 1@2 required output is 1@3 Cloud you please help... (7 Replies)
Discussion started by: bmk123
7 Replies

2. Shell Programming and Scripting

awk and substr

Hello All; I have an input file 'abc.txt' with below text: 512345977,213458,100021 512345978,213454,100031 512345979,213452,100051 512345980,213455,100061 512345981,213456,100071 512345982,213456,100091 512345983,213457,100041 512345984,213451,100011 I need to paste the first field... (10 Replies)
Discussion started by: mystition
10 Replies

3. Shell Programming and Scripting

Replace delimeter between quotes with \| using sed or awk

I've a pipe delimited data in a file of size 3GB. if the text data conatins pipe delimiter that will be enclose with double quotes. I have to replace delimiter which exists between double quotes with #%@#%@#%@ using awk or sed. can some one provide a better and efficient solution to me. The below... (4 Replies)
Discussion started by: BrahmaNaiduA
4 Replies

4. Shell Programming and Scripting

awk substr

Hello life savers!! Is there any way to use substr in awk command for returning one part of a string from declared start and stop point? I mean I know we have this: substr(string, start, length) Do we have anything like possible to use in awk ? : substr(string, start, stop) ... (9 Replies)
Discussion started by: @man
9 Replies

5. Shell Programming and Scripting

Substr with awk

Hi to all, I'm here again, cause I need your help to solve another issue for me. I have some files that have this name format: date_filename.csv In my shell I must rename each file removing the date so that the file name is filename.csv To do this I use this command: fnames=`ls ${fname}|... (2 Replies)
Discussion started by: leobdj
2 Replies

6. Shell Programming and Scripting

awk substr

HI I am using awk and substr function to list out the directory names in the present working directory . I am using below code ls -l | awk '{ if ((substr($1,1,1)) -eq d) {print $9 }}' But the problem is i am getting all the files and directories listed where as the requirement i wrote... (7 Replies)
Discussion started by: prabhu_kumar
7 Replies

7. Shell Programming and Scripting

Count the delimeter from a file and delete the row if delimeter count doesnt match.

I have a file containing about 5 million rows, in the file there are some records which has extra delimiter at random position. (we dont know the positions), now we have to Count the delimeter from each row and if the count of delimeter is not matching then I want to delete those rows from the... (5 Replies)
Discussion started by: Akumar1
5 Replies

8. Shell Programming and Scripting

Need Awk command to get part of string based on delimeter

HI, Need awk command to get date and time alone from Input : "15:29:15 28.08.2010|SCHEDULE: Started program POSG1" Output expected : "15:29:15 28.08.2010" Please help. (9 Replies)
Discussion started by: shanneykar
9 Replies

9. Shell Programming and Scripting

awk substr

Hi I have multiple files that name begins bidb_yyyymm. (yyyymm = current year month of file creation). What I want to do is look at the files and where yyyymm is older than 1 month I want to remove the file from the server. I was looking at looping through the files and getting the yyyymm... (2 Replies)
Discussion started by: colesga
2 Replies

10. Shell Programming and Scripting

How to use awk substr ?

Hi all, I have a flatfile I would like to get ext = 7950 , how do I do that ? if ($1 == "CTI-ProgramStart") { ext = substr($9,index($9,"Extension")+11,4); But why it is not working ???? Please help . Thanks (1 Reply)
Discussion started by: sabercats
1 Replies
Login or Register to Ask a Question