remove values from a file present in another file using bash - linux

I have a tab separated file A containing several values per row:
A B C D E
F G H I
J K L M
N O P
Q R S T
U V
X Y Z
I want to remove from file A the elements contained in the following file B:
A D
J M
U V
resulting in a file C:
B C E
F G H I
K L
N O P
Q R S T
X Y Z
Is there a way of doing this using bash?

In case the entries do not contain any special symbols for sed (for instance ()[]/\.*?+) you can use the following command:
mapfile -t array < <(<B tr '\t' '\n')
(IFS='|'; sed -r "s/(${array[*]})\t?//g;/^$/d" A > C)
This command reads file B into an array. From the array a sed command is constructed. The sed command will filter out all entries and delete blank lines.
In your example, the constructed command ...
sed -r 's/(A|D|J|M|U|V)\t?//g;/^$/d' A > C
... generates the following file C (spaces are actually tabs)
B C E
F G H I
K L
N O P
Q R S T
X Y Z

awk solution:
awk 'NR == FNR{ pat = sprintf("%s%s|%s", (pat? pat "|":""), $1, $2); next }
{
gsub("^(" pat ")[[:space:]]*|[[:space:]]*(" pat ")", "");
if (NF) print
}' file_b file_a
The output:
B C E
F G H I
K L
N O P
Q R S T
X Y Z

Related

How to add line in code in python console

How to add new line in python. for example, I would like to print the rest of a sentence in a new line. but instead of putting "\n", I will automate it to type to a new line for every six words.
Morse code translator
sth like:
def wrapper(words, n):
to_print = ''
for i in range(0, len(words.split()), n):
to_print += ' '.join(words.split()[i:i+n]) + '\n'
return to_print
and result is:
print(wrapper('a b c d e f g h i j k l m n o p r s t u w x y z', 6))
a b c d e f
g h i j k l
m n o p r s
t u w x y z

How to Pass More then 10 argument in ShellScript

sed -e "s/${MyToken}/${arg}/g" file
Value Of 'arg' working fine till 9th argument.after 10th arguments its failing
Here is an example :
#!/bin/ksh
echo There is $# parameters
while [ $# -ne 0 ]; do
echo $1
shift
done
The output :
Will:/home> ./script a b c d e f g h i j k l m n o p q r s t u v w x y z
There is 26 parameters
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
$#count the number of parameters passed to your script.
Shift is moving the param to the left so after one loop $1 become b and so on.

Split a single row of data (dat file) into multiple columns

I want to split a row of data into multiple columns like
a.dat
A B C D E F G H I J K L M N O P Q R S T U
into
b.dat
A B C D E F G
H I J K L M N
O P Q R S T U
I have tried using the pr function
pr -ts" " --columns 7 --across a.dat > b.dat
But it doesn't work, b.dat is the same as a.dat
I like fold for these thingies:
$ fold -w 14 file
A B C D E F G
H I J K L M N
O P Q R S T U
With -w you set the width you desire to have.
Although xargs is more useful if you want to split based on number of fields instead of characters:
$ xargs -n 7 < file
A B C D E F G
H I J K L M N
O P Q R S T U
Regarding your attempt in pr: I don't really know why it is not working, although from some examples I see it doesn't look like the tool for such job.

strange uniq output on Mac

Here is the input file and output, I think characters like c and g should not be output?
$ uniq c.txt
a
g
b
g
c
v
c
$ cat c.txt
a
g
b
b
g
g
c
v
c
thanks in advance,
Lin
From the uniq man page:
Repeated lines in the input will not be detected if they are not
adjacent, so it may be necessary to sort the files first.
macbook:stackoverflow joeyoung$ cat c.txt
a
g
b
b
g
g
c
v
c
macbook:stackoverflow joeyoung$ uniq c.txt
a
g
b
g
c
v
c
macbook:stackoverflow joeyoung$ sort -u c.txt
a
b
c
g
v
macbook:stackoverflow joeyoung$ sort c.txt | uniq
a
b
c
g
v

Write character/line from a string in bash? [duplicate]

This question already has answers here:
How to perform a for loop on each character in a string in Bash?
(16 answers)
Closed 8 years ago.
I want to get this string -> Example example1
in this form:
E
x
a
m
p
l
e
e
x
a
m
p
l
e
1
Use fold utility with width=1:
echo 'Example example1' | fold -w1
E
x
a
m
p
l
e
e
x
a
m
p
l
e
1
Another option is grep -o:
echo 'Example example1' | grep -o .
E
x
a
m
p
l
e
e
x
a
m
p
l
e
1
Using standard unix tools, you can do, for example:
echo "Example example1" | sed 's/\(.\)/\1\n/g'
Using pure bash:
echo "Example example1" | while read -r -n 1 c ; do echo "$c"; done

Resources