Complex output Sed
Let me start by saying that I know that there is probably a much easier way to do this. But this is what I have and yes, I hope I can make some improvements and / or simplifications at the end.
Aim from now on
To double the space, the output is stored in the variable $tmp
below and at the individual line number of each line. Doing this should give me each set of executable commands on separate lines.
Problem 1
I've tried everything I can think of to double the space that's in a variable. Move the double space command around and change the command itself.
To specify the number of files I tried to modify, follow these steps:
sed = filename | sed 'N; s/^/ /; s/ *\(.\{6,\}\)\n/\1 /
And of course I experimented with awk, but there was no progress.
Short-term goals
- Execute these commands one at a time and compare the difference in bytes. Or just some general difference. I know that at this point I can use the diff command with
file1 file2
and then store the result in some variable like:"$diffA $diffB"
and update it each time using a loop for the likelihood that more than 2 files are passed as arguments. - To tell the difference between each command line. And repeat something like:
line 1 in file1 is different from line 1 in file2, check it: "$ diffA $ diffB"
Here's what I have so far, its beginning:
#!/bin/bash
FILE="$1"
echo "You Entered $FILE"
if [ -f $FILE ]; then
tmp=$(cat $FILE | sed '/./!d' | sed -n '/regex/,/regex/{/regex/d;p}'| sed -n '/---/,+2!p' | sed -n '/#/!p' | sed 's/^[ ]*//' | sed -e\
s/[^:]*:// | sed -n '/regex /!p' | sed -n '/regex /!p' | sed -n '/"regex"/,+1!p' | sed -n '/======/!p' | sed -n '/regex/!p' | sed -n '/regex\
\r/!p' | sed -n '/regex /!p' )
fi
MyVar=$(echo $tmp)
echo "$MyVar | sed G"
FILE2="$2"
if [ -f $FILE2 ]; then
if [ -f $FILE2 ]; then
tmp2=$(cat $FILE2 | sed -n '/regex/,/regex/{/regex\ S/d;p}' |\
sed -n '/#/!p' | sed -e s/[^:]*:// | sed /--------/,+10d)
fi
echo "$tmp2"
Any help is greatly appreciated.
The following awk script will double the output and the number of lines:
awk ' { print NF " " $0; print ""; }'
Your job is to assign this to a variable:
( echo a; echo b) | awk ' { print NR " " $0; print ""; }'
gives:
1 a
2 b
but
tmp=$(( echo a; echo b) | awk ' { print NR " " $0; print ""; }')
echo "$tmp"
gives
1 a 2 b
because using $ () will replace newlines with spaces. You will have to use a temporary file:
tmp=$(mktemp)
awk ' { print NR " " $0; print ""; }' $FILE > $tmp
cat $tmp # Do something with the file
rm $tmp # Don't forget to clean up after yourself.
An alternative to using a temporary file is to use quotes to suppress newline / space substitution:
tmp="$(echo a; echo b)"
echo "$tmp"
This might work for you:
a=$(printf "aaa %d\n" {1..5} | sed = | sed 'N;s/\n//;G')
echo $a
1aaa 1 2aaa 2 3aaa 3 4aaa 4 5aaa 5
echo "$a"
1aaa 1
2aaa 2
3aaa 3
4aaa 4
5aaa 5
Explanation:
-
sed =
inserts a line number, adds a new line, and then adds a line -
sed 'N's/\n//;G'
Adds a new line, reads another line, and appends it.s/\n//
removes the inline newline.G
appends a new line to the end of the line. At the end of the loop, a new line is reattached before printing.