Awk: split and gensub query

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Awk: split and gensub query
# 1  
Awk: split and gensub query

Hi All,

Thanks for answering my previous question. Could you please explain the highlighted code?

awk -v pos='9 27 39 54 59 64 71 78 83 103 108' 'BEGIN{split(pos,var)} {for (i in var) $0=gensub(/./,"|",var[i])} 1' test.txt | head

I understood that the split function splits the pos string into an array but I am confused on the usage of gensub function, especially var[i]. It wold be helpful if there is any tool which I use to visualize the steps.


Last edited by mrcool4; 08-13-2019 at 06:09 AM.. Reason: Added more info
# 2  
You are right - the pos variable is split into the var array. Then, in all input lines ($0), gensub (a specific gawk function) replaces the characters (no matter what they are!) at the positions in var by the pipe symbol, as said in man gawk:

gensub(regexp, replacement, how [, target])
Search the target string target for matches of the regular expression regexp. If how is a string beginning with ‘g’ or ‘G’ (short for “global”), then replace all matches of regexp with replacement. Otherwise, treat how as a number indicating which match of regexp to replace. Treat numeric values less than one as if they were one. If no target is supplied, use $0. Return the modified string as the result of the function. The original target string is not changed.
This User Gave Thanks to RudiC For This Post:
# 3  
Thanks Rudic. Got it. I have been trying to understand by simplifying the function and finally understood by trying with a few examples.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #314
Difficulty: Easy
Pointers were not used in the original C programming language and were added later in in C++.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Query on awk

Hello, I am trying to find the RAM usage for one of the process from a group of servers. xx-Process name The command i used by logging to the individual server is as below ps aux | grep xx | awk '{sum=sum+$6}; END {print sum/1024 " MB"}' And the result is 0.824219 MB This... (4 Replies)
Discussion started by: nextStep
4 Replies

2. Programming

Need sql query to string split and normalize data

Hello gurus, I have data in one of the oracle tables as as below: Column 1 Column 2 1 NY,NJ,CA 2 US,UK, 3 AS,EU,NA fyi, Column 2 above has data delimited with a comma as shown. I need a sql query the produce the below output in two columns... (5 Replies)
Discussion started by: calredd
5 Replies

3. Shell Programming and Scripting

awk split and awk calculation in the same command

I am trying to run the awk below. My question is when I split the input, then run anotherawk to perform a calculation using that splitas the input there are no issues. When I try to combine them the output is not correct, is the split not working or did I do it wrong? Thank you :). input ... (8 Replies)
Discussion started by: cmccabe
8 Replies

4. Shell Programming and Scripting

Gawk gensub, match capital words and lowercase words

Hi I have strings like these : Vengeance mitt Men Vengeance gloves Women Quatro Windstopper Etip gloves Quatro Windstopper Etip gloves Girls Thermobite hooded jacket Thermobite Triclimate snow jacket Boys Thermobite Triclimate snow jacket and I would like to get the lower case words at... (2 Replies)
Discussion started by: louisJ
2 Replies

5. Shell Programming and Scripting

awk query

Hi, I have a sample file in the following format. 000013560240|000013560240|001|P|155|99396|0||SS00325665| 000013560240|000013560240|002|P|17|99000|0||SS00325665| 000013560240|000013560240|002|F|-17|99000|0|R|SS00325665| 000013560240|000013560240|003|P|20|82270|0||SS00325665|... (3 Replies)
Discussion started by: nua7
3 Replies

6. Shell Programming and Scripting

awk to split one field and print the last two fields within the split part.

Hello; I have a file consists of 4 columns separated by tab. The problem is the third fields. Some of the them are very long but can be split by the vertical bar "|". Also some of them do not contain the string "UniProt", but I could ignore it at this moment, and sort the file afterwards. Here is... (5 Replies)
Discussion started by: yifangt
5 Replies

7. UNIX for Dummies Questions & Answers

gensub and arraywith awk

Hi ! I would need some help for something I don't understand :confused: input: 111|2 Y Z blue. 333|4 W X blue.; 5 Y Z red. 666|7 W X red.; 8 Y Z blue. 999|10 U V red.; 11 W X blue.; 12 Y Z red. From $2, I would like to remove the sub-strings containing "blue" (and the... (4 Replies)
Discussion started by: beca123456
4 Replies

8. Shell Programming and Scripting

AWK Query

i have a log file where in i have 3 columns a 11 test1 b 22 test2 in a script, i want to pass first column as an argument to awk so that i can get the second and third column respectively Example : If i pass ($1 i.e 'a' to an awk, the result should be 11 test1) i tried different awk... (2 Replies)
Discussion started by: vkca
2 Replies

9. Shell Programming and Scripting

gawk and gensub

Hi, $ echo "Hellooo" | gawk '{print gensub(/o{3}/, "z", 1)}' doesn't return "Hellz" as expected while: $ echo "Hellooo" | awk '{print gensub(/o+/, "z", 1)}' produces "Hellz" correctly. Are the {m,n} quantifiers not supported in gensub? I know that sub or gsub could do the job. It's just an... (2 Replies)
Discussion started by: ripat
2 Replies

10. UNIX for Dummies Questions & Answers

Split a file with no pattern -- Split, Csplit, Awk

I have gone through all the threads in the forum and tested out different things. I am trying to split a 3GB file into multiple files. Some files are even larger than this. For example: split -l 3000000 filename.txt This is very slow and it splits the file with 3 million records in each... (10 Replies)
Discussion started by: madhunk
10 Replies

Featured Tech Videos