how can i delete columns beginning and ending with parenthesis in a file
Expectd Input - content of input.txt
ABC (BCD) EFG
Usage
awk '{print $0" "}' foo.txt | awk -f foo.awk
foo.awk
BEGIN {
RS=ORS=" "
}
{
n=length($0)
if (!n) next
split($0, s, "")
}
s[1]=="(" && s[n]==")" {
# it is column like (abcd), skip it
next
}
s[1]=="(" {
# stop printing
f=1
}
!f {
print $0
}
s[n]==")" {
# start printing again
f=0
}
Based on the solution from @slitvinov:
BEGIN {
RS = "[[:space:]]"
ORS = ""
eat = 0
}
/^\(.*\)$/ {
next
}
/^\(/ {
eat = 1
next
}
/\)$/ {
if (eat) {
eat = 0
next
}
}
{
if (eat)
next
print $0 RT
}
That to an .awk
file and awk -f foo.awk foo.txt
gives:
ABC EFG
BCD
DEF BCD
EFG HI(JKL)
ABC EFG LMN
But I think it could be done simpler...
The simplest thing that I can assemble is:
perl -pe 'BEGIN { undef $<; } s/\s(\(.*?\)(\s))+/\2/cgs' foo.txt
Sorry for Perl but it's in POSIX and it has regular expressions powerful enough to cover the case.
Ah, and it can't handle if the file starts with a parenthesis. If it ends with one, it's fine as long as there's newline after it. If that's a problem, then the easiest solution would be to just add a temporary space.