awk interview questions
Top awk frequently asked interview questions
Here is example file
somestuff...
all: thing otherthing
some other stuff
What I want to do is to add to the line that starts with all:
like this:
somestuff...
all: thing otherthing anotherthing
some other stuff
I probably can do this using sed, but I am not really good in sed, so could anyone help with it?
Source: (StackOverflow)
I want to print the second last column or field in awk. The number of fields is variable. I know that I should be able to use $NF
but not sure how it can be used.
And this does not seem to work:
awk ' { print ( $NF-- ) } '
Source: (StackOverflow)
How to split the string when it contains pipe symbols |
in it.
I want to split them to be in array.
I tried
echo "12:23:11" | awk '{split($0,a,":"); print a[3] a[2] a[1]}'
Which works fine. If my string is like "12|23|11"
then how do I split them into array?
Source: (StackOverflow)
I have a text file with the following format. The first line is the "KEY" and the second line is the "VALUE".
KEY 4048:1736 string
3
KEY 0:1772 string
1
KEY 4192:1349 string
1
KEY 7329:2407 string
2
KEY 0:1774 string
1
I need the value in the same line as of the key. So the output should look like this...
KEY 4048:1736 string 3
KEY 0:1772 string 1
KEY 4192:1349 string 1
KEY 7329:2407 string 2
KEY 0:1774 string 1
It will be better if I could use some delimiter like $ or ,
KEY 4048:1736 string , 3
How do I merge two lines into one?
Source: (StackOverflow)
I ended up writing a quick little script for this in Python, but I was wondering if there was a utility you could feed text into which would prepend each line with some text -- in my specific case, a timestamp. Ideally, the use would be something like:
$ cat somefile.txt | prepend-timestamp
(Before you answer sed, I tried this:
$ cat somefile.txt | sed "s/^/`date`/"
but that only evaluates the date command once when sed is executed, so the same timestamp is incorrectly prepended to each line.)
Source: (StackOverflow)
All I want is the last two columns printed. thanks I am surprised this question wasn't yet asked.
Source: (StackOverflow)
has anyone an idea how an awk script (presumably a one-liner) for removing a BOM would look like?
Specification:
- print every line after the first (
NR > 1
)
- for the first line: If it starts with
#FE #FF
or #FF #FE
, remove those and print the rest
Source: (StackOverflow)
I am constantly learning new tools, even old fashioned ones, because I like to use the right solution for the problem.
Nevertheless, I wonder if there is still any reason to learn some of them. awk
for example is interesting to me, but for simple text processing, I can use grep
, cut
, sed
, etc. while for complex ones, I'll go for Python.
Now I don't mean that's it's not a powerful and handy tool. But since it takes time and energy to learn a new tool, is it worth it ?
Source: (StackOverflow)
I have two files:
file 1
dsf
sdfsd
dsfsdf
file 2
ljljlj
lkklk
dsf
sdfsd
dsfsdf
I want to display what is in file 2 but not in file 1, so file 3 should look like
ljljlj
lkklk
Source: (StackOverflow)
just want to know what are the main differences among them? and the power of each language (where it's better to use it).
Edit: it's not "vs." like topic, just information.
Source: (StackOverflow)
If I have an awk command
pattern { ... }
and pattern uses a capturing group, how can I access the string so captured in the block?
Source: (StackOverflow)
I have some files that I'd like to delete the last newline if it is the last character in a file. 'od -c' shows me that the command I run does write the file with a trailing new line:
0013600 n t > \n
I've tried a few tricks with sed but the best I could think of isn't doing the trick:
sed -e '$s/\(.*\)\n$/\1/' abc
Any ideas how to do this?
Source: (StackOverflow)
- What is the difference between awk
and sed ?
- What kind of application are best use
cases for sed and awk tools ?
Source: (StackOverflow)
I'm trying to parse json returned from a curl request, like sp:
curl 'http://twitter.com/users/username.json' | sed -e 's/[{}]/''/g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}'
I have it set working where it splits the json into fields, i.e. the above returns
% ...
"geo_enabled":false
"friends_count":245
"profile_text_color":"000000"
"status":"in_reply_to_screen_name":null
"source":"web"
"truncated":false
"text":"My status"
"favorited":false
% ...
But what I would like to do is grab a specific field (denoted by the -v k=text) and only print that.
Any ideas?
Source: (StackOverflow)
I want the variable sum/NR to be printed side-by-side in each iteration. How do we avoid awk from printing newline in each iteration ? In my code a newline is printed by default in each iteration
for file in cg_c ep_c is_c tau xhpl
printf "\n $file" >> to-plot.xls
for f in 2.54 1.60 800
awk '{sum+=$3}; END {print sum/NR}' ${file}_${f}_v1.xls >> to-plot-p.xls
done
done
I want the output to appear like this
cg_c ans1 ans2 ans3
ep_c ans1 ans2 ans3
is_c ans1 ans2 ans3
tau ans1 ans2 ans3
xhpl ans1 ans2 ans3
my current out put is like this
**cg_c**
ans1
ans2
ans3
**ep_c**
ans1
ans2
ans3
**is_c**
ans1
ans2
ans3
**tau**
ans1
ans2
ans3
**xhpl**
ans1
ans2
ans3
Source: (StackOverflow)