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 1 year ago.
Improve this question
Everyone, what command line should I use to achieve this effect?
This is trying to use awk and sed but failed. Please advise.
Original:
server=/example-a.com/127.0.0.1#5353
ipset=/example-a.com/router
server=/example-b.com/127.0.0.1#5353
ipset=/example-b.com/router
server=/example-c.com/127.0.0.1#5353
ipset=/example-c.com/router
Achieve effect:
server=/example-a.com/127.0.0.1#5353
server=/example-a.com/127.0.0.2#5354
ipset=/example-a.com/router
server=/example-b.com/127.0.0.1#5353
server=/example-b.com/127.0.0.2#5354
ipset=/example-b.com/router
server=/example-c.com/127.0.0.1#5353
server=/example-c.com/127.0.0.2#5354
ipset=/example-c.com/router
Since you need arithmetic, I would go with awk:
awk -F'[#.]' '/^server/ { print $0, ORS $1"."$2"."$3"."$4"."$5+1"#"$6+1 } !/^server/'
Output:
server=/example-a.com/127.0.0.1#5353
server=/example-a.com/127.0.0.2#5354
ipset=/example-a.com/router
server=/example-b.com/127.0.0.1#5353
server=/example-b.com/127.0.0.2#5354
ipset=/example-b.com/router
server=/example-c.com/127.0.0.1#5353
server=/example-c.com/127.0.0.2#5354
ipset=/example-c.com/router
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 have a CSV file that I want to search and find the first occurrence then replace the second occurrence with the first
Here is an example of courses.csv
"Foo","Bar",course foo,Lorem
"Baz","Foo",course bar,Lorem
In the above CSV, I want to replace the second string in the double quotes with the first string in the double quotes
After the operation, the results should be as follow
"Foo","Foo",course foo,Lorem
"Baz","Baz",course bar,Lorem
How can I achieve this preferably on bash?
Assuming you'll always want to put the first field in place of the second field, I'd urge you to try the following:
awk 'BEGIN{FS=",";OFS=","}{$2=$1;print $0}' yourfile.csv
alternatively
awk -v FS="," -v OFS="," '{$2=$1;print $0}' yourfile.csv
this is not in-place: that would require some more effort, but the gist is this.
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 4 years ago.
Improve this question
Remove all lines that don't contain a letter from the alphabet (upper or lower case)
Input :
34
76
0hjjAby68xp
H5e
895
Output :
0hjjAby68xp
H5e
With GNU grep:
grep '[[:alpha:]]' file
or GNU sed:
sed '/[[:alpha:]]/!d' file
Output:
0hjjAby68xp
H5e
Using awk:
$ awk '/[[:alpha:]]/' file
0hjjAby68xp
H5e
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 6 years ago.
Improve this question
I am trying to write a script to grep output from a range based on text, not line numbers.
For instance, in my text file, I want to grep the output starting with $hostname and capture everything in between $endText and then output the data in between those to a file named $hostname.txt.
Since you didn't provide any details, here is the boiler plate.
$ sed -n '/start/,/end/p' file > outputfile
or
$ awk '/start/,/end/' file > outputfile