Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
n=4;
echo "abcd" awk '{print substr($0,$n,1);}'
I want to get the substring by the usage of variable but I am not getting please help
$ n=4; echo "abcd" | awk -v n="$n" '{print substr($0,n,1);}'
d
Possibly, it is clearer to have two different variable names:
$ n=4; echo "abcd" | awk -v m="$n" '{print substr($0,m,1);}'
d
Here, n is a shell variable and m is an awk variable. The -v option is used to assign the awk variable m to have the value of the shell variable n.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Eg.
abc_def_ghi_xyz
uvw_mno_gab_xyz
bac_cab_lmn_xyz
should be replaced with
ABC_xyz
ABC_xyz
ABC_xyz
How to do using awk, %s and sed commands ?
Using %s:
:%s/*_xyz/ABC_xyz/g
I would also suggest looking at :h about vim search and replace.
Using awk you can do:
awk -F_ '{print "ABC_"$NF}' file
Where:
-F_ .............. _ as field separator
"ABC_" ............ literal ABC_
$NF ............... last field
Using vim:
:%s/.*\ze_xyz/ABC
Using sed:
sed -r 's/.*(_xyz)/ABC\1/g' file
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How do I change 0.1.88 to 1.0.88 using awk functionality?
I tried below:
ori_value=0.1.88
value=echo $ori_value | awk -F'.' -v OFS=. '++$(NF-1)'
this just updates to 0.2.88
but I want 1.0.88
Please help me with this
Thank you in advance
Could you please try following, written and tested with shown samples in GNU awk.
echo "0.1.88" | awk 'BEGIN{FS=OFS="."} {print $2,$1,$3}'
OR as per OP's shown variable:
echo "$ori_value" | awk 'BEGIN{FS=OFS="."} {print $2,$1,$3}'
2nd solution: With sed could you please try following in sed with shown samples.
echo "0.1.88" | sed 's/\([^.]*\)\.\([^.]*\)\.\([^.]*\)/\2.\1.\3/'
You could re-assign the fields, like so:
awk -F'.' -v OFS=. '{$1=$2; $2=0; print $0}'
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
i search for this string abcdefgh in a very large file like this and i don't know at which position the new line begin. My first thought was to remove all \n, but the file is over 3 gb ... I think there is smart way to do this (sed, awk, ...?)
efhashivia
hjasjdjasd
oqkfoflABC
DEFGHqpclq
pasdpapsda
Assuming that your search string cannot expand into more than 2 lines, you can use this awk:
awk -v p="ABCDEFGH" 's $0 ~ p {print NR,s $0} {s=$0}' file
Or you can paste each line with its next one, and grep the result. This way you have to create a file with double size of your large input.
tail -n +2 file | paste -d '' file - > output.txt
> cat output.txt
efhashiviahjasjdjasd
hjasjdjasdoqkfoflABC
oqkfoflABCDEFGHqpclq
DEFGHqpclqpasdpapsda
pasdpapsda
> grep -n ABCDEFGH output.txt
3:oqkfoflABCDEFGHqpclq
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
printing nth column and removing some extra characters
cat test.log
12-22-2018-00.log:[1545418720] SERVER ALERT: irqz1;response from
Server(in ms) : 387
12-22-2018-00.log:[1545118720] PING ALERT: irqz2;Server is up since
902 days
grep -i 'SERVER' test.log |awk '{print $1,$NF}'
12-22-2018-00.log:[1545418720] 387
grep -i 'PING' test.log |awk '{print $1,$NF-1}'
12-22-2018-00.log:[1545118720] 902
Actual result i want is:
Server 1545418720 387
Ping 1545118720 902
So in case of Server alert it will select the unix timestamp and the last column and in case of ping alert, it will select unix timestamp and 2nd to the right column.
Could you please try following(GNU awk).
awk -v IGNORECASE=1 '/server alert/{gsub(/.*\[|\]$/,"",$1);print $1,$NF;next} /ping alert/{gsub(/.*\[|\]$/,"",$1);print $1,$(NF-1)}' Input_file
OR
awk 'tolower($0) ~ /server alert/{gsub(/.*\[|\]$/,"",$1);print $1,$NF}' Input_file
AND
awk 'tolower($0) ~ /ping alert/{gsub(/.*\[|\]$/,"",$1);print $1,$(NF-1)}' Input_file
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a file with the following format:
AACCCGTAGATCCGAACTTGTG
ACCCGTAGATCCGAACTTGTG
CCGTAGATCCGAACTTGTG
CGTAGATCCGAACTTGT
I want to give a header to each line, using awk, where the header is equal to the line that follows, like this:
>AACCCGTAGATCCGAACTTGTG
AACCCGTAGATCCGAACTTGTG
>ACCCGTAGATCCGAACTTGTG
ACCCGTAGATCCGAACTTGTG
>CCGTAGATCCGAACTTGTG
CCGTAGATCCGAACTTGTG
>CGTAGATCCGAACTTGT
CGTAGATCCGAACTTGT
Simply:
$ awk '{print ">"$0;print}' file
>AACCCGTAGATCCGAACTTGTG
AACCCGTAGATCCGAACTTGTG
>ACCCGTAGATCCGAACTTGTG
ACCCGTAGATCCGAACTTGTG
>CCGTAGATCCGAACTTGTG
CCGTAGATCCGAACTTGTG
>CGTAGATCCGAACTTGT
CGTAGATCCGAACTTGT
Or:
$ awk '{printf ">%s\n%s\n",$0,$0}' file
>AACCCGTAGATCCGAACTTGTG
AACCCGTAGATCCGAACTTGTG
>ACCCGTAGATCCGAACTTGTG
ACCCGTAGATCCGAACTTGTG
>CCGTAGATCCGAACTTGTG
CCGTAGATCCGAACTTGTG
>CGTAGATCCGAACTTGT
CGTAGATCCGAACTTGT
The -v flag allows you to set a variable. Then for each line in the file print that variable followed by the line, and then the line itself.
awk -v c=">" '{ print c $0; print $0; }' <file>