I have the following code:
#!/bin/bash
FSTAB=` grep -v "^#" /etc/fstab | grep . | grep -v "swap" | grep -v "UUID" | awk '{print $1,$2,$3}'`
for i in $FSTAB
do
echo "$i"
done
this returns me:
/dev/mapper/centos-root
/
xfs
/dev/sdb2
/hdos
xfs
The problem is that later I want to compare it, so it is returning it to me with line breaks and I want it to return it to me in a single line without breaks, that is to say:
/dev/mapper/centos-root / xfs /dev/sdb2 /hdos xfs
If the result returned by a simple
echo "$FSTAB"
isSo you can remove or replace the "\n" line breaks with
tr
.Or with
sed
The explanation (which I don't know) can be found in another answer .
In both cases, the loop
for
is unnecessary.And now we are all on the train
awk
, an option that I think is viable is:From a file of the form.
Or traversing the array of fields.
Or furthermore, following the advice of
next
.Or better by negating the regex.
Which you can also solve with
cut
andxargs
.Since you're using
awk
, you can let it do it itself, preventing it from adding a line break at the end of each one. changeby
And, continuing with
awk
, you can simplify all those command interconnections:The syntax
/EXPRESION-REGULAR/{ ... }
executes the code only if the regular expression matches, while{next}
discarding the current line to continue with the next.