I want to remove characters " ./ " and " .txt " from my file using sub and awk
This is what my input looks in file
./file_name.txt|1230
I want the output to be
file_name|1230
So far This is what I have written.I will use the output of this gsub and pass it to awk and print it.Rest of my code is working except this .
gsub ( "[./txt]","" )
Use gsub
instead and remember to escape .
and /
like \\.
and \\/
:
$ echo "./file_name.txt|1230" | awk '{gsub(/\.\/|\.txt/,"")}1'
file_name|1230
Since you said you need to replace in a file you can use,
awk '{gsub(/\.txt|\.\//, "")}1' file_name > tmp && mv tmp file_name
or you can use gawk
which is much simpler,
gawk -i inplace '{gsub(/\.txt|\.\//, "")}1' file_name
This answer explains what 1
at the end of the statements means.
您提到了awk
,而另一个答案已经回答了它,但是这是您可以使用sed
做到这一点的一种方法:
sed 's|\./||g; s|\.txt||g' <<< "./file_name.txt|1230"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.