Remove a Specific Character Using Awk or Sed

Remove a specific character using awk or sed

Use sed's substitution: sed 's/"//g'

s/X/Y/ replaces X with Y.

g means all occurrences should be replaced, not just the first one.

How can I remove a string after a specific character ONLY in a column/field in awk or bash?

You need to simply correct your regex.

awk '{sub(/;.*/,"",$2)} 1' Input_file

In case you have Input_file TAB delimited then try:

awk 'BEGIN{FS=OFS="\t"} {sub(/;.*/,"",$2)} 1' Input_file

Problem in OP's regex: OP's regex ;[*] is looking for ; and *(literal character) in 2nd field that's why its NOT able to substitute everything after ; in 2nd field. We need to simply give ;.* which means grab everything from very first occurrence of ; till last of 2nd field and then substitute with NULL in 2nd field.

Awk remove specific characters

echo "\n" | nc myserver.com 1234 | awk -F "," '{print substr($3,6)}'

How to remove n characters from a specific column using sed/awk/perl

perl -anE'$F[3] =~ s/.{7}//; say join "\t", @F' data.txt

or

perl -anE'substr $F[3],0,7,""; say join "\t", @F' data.txt

awk or sed to remove text in file before character and then after character

awk -F'[\t()]' '{OFS="\t"; print $1, $2, $3, $5 $6}' file

Output:


chr4 100009839 100009851 ADH5_1
chr4 100006265 100006367 ADH5_2
chr4 100003125 100003267 ADH5_3

Remove \r\ character from String pattern matched in AWK

File names can contain spaces, including \rs, blanks and tabs, so to do this robustly you can't remove all \rs with gsub() and you can't rely on there being any field, e.g. $2, that contains the whole file name.

If your input fields are tab-separated you need:

awk '/DATAFILE/ { sub(/[^\t]+\t/,""); sub(/\r$/,""); print }' file

or this otherwise:

awk '/DATAFILE/ { sub(/[^[:space:]]+[[:space:]]+/,""); sub(/\r$/,""); print }' file

The above assumes your file names don't start with spaces and don't contain newlines.

To test any solution for robustness try:

printf 'DATAFILE\tfoo \r bar\r\n' | awk '...' | cat -TEv

and make sure that the output looks like it does below:

$ printf 'DATAFILE\tfoo \r\tbar\r\n' | awk '/DATAFILE/ { sub(/[^\t]+\t/,""); sub(/\r$/,""); print }' | cat -TEv
foo ^M^Ibar$

$ printf 'DATAFILE\tfoo \r\tbar\r\n' | awk '/DATAFILE/ { sub(/[^[:space:]]+[[:space:]]+/,""); sub(/\r$/,""); print }' | cat -TEv
foo ^M^Ibar$

Note the blank, ^M (CR), and ^I (tab) in the middle of the file name as they should be but no ^M at the end of the line.

If your version of cat doesn't support -T or -E then do whatever you normally do to look for non-printing chars, e.g. od -c or vi the output.

How can I remove characters from filenames with awk or sed?

No need for sed or awk, just bash parameter expansion:

for f in *.txt
do
dest=${f#kkk}
dest=${dest%_A.txt}.txt
mv "$f" "$dest"
done


Related Topics



Leave a reply



Submit