Home > Blockchain >  Adding content before specific line number(s) in a text file using Shell Scripting
Adding content before specific line number(s) in a text file using Shell Scripting

Time:10-01

So I have a text file that looks something like this:

a1
b1
c1 
a2 
b2 
c2
.
.
.

Now I want to index each component of this text file such that is looks like this:

0:
a1
b1
c1
1:
a2
b2
c2
.
.
.

I tried developing several shell scripts but unfortunately couldn't achieve the above-mentioned objective. It would be really great if someone could point me in the right direction as to how to perform the above-mentioned task using shell scripting. Thanks!

PS: I'm a macOS user ( Just letting you know since awk/sed commands perform a bit differently on macOS & Linux )

CodePudding user response:

$ cat input
a1
b1
c1
a2
b2
c2
.
.
.
$ awk '/^a/{printf "%d:\n", counter  }1' input
0:
a1
b1
c1
1:
a2
b2
c2
.
.
.

Or:

awk '/^a/{print counter ":" }1' input, but printf feels cleaner.

CodePudding user response:

Use this Perl one-liner, which relies on the assumption that there are exactly 3 lines per each group that shares the same index:

echo 'a1 b1 c1 a2 b2 c2 a3 b3 c3' | xargs -n1 > in_file
perl -lpe '$i = (($. - 1) / 3); print "$i:" if ! (($. - 1) % 3);' in_file > out_file

Contents of out_file:

0:
a1
b1
c1
1:
a2
b2
c2
2:
a3
b3
c3

The Perl one-liner uses these command line flags:
-e : Tells Perl to look for code in-line, instead of in a file.
-p : Loop over the input one line at a time, assigning it to $_ by default. Add print $_ after each loop iteration.
-l : Strip the input line separator ("\n" on *NIX by default) before executing the code in-line, and append it when printing.

$. : Current input line number.

SEE ALSO:
perldoc perlrun: how to execute the Perl interpreter: command line switches

CodePudding user response:

Using any awk in any shell on every Unix box:

$ awk '{key=$0; gsub(/[^0-9] /,"",key)} key != prev{print (key-1)":"} {print; prev=key}' file
0:
a1
b1
c1
1:
a2
b2
c2
  • Related