Add number prepend certain lines

from

a(depends: )
bb(depends:lmbench )
cc(depends: )
    CONFIG_DEFAULT1=y
    CONFIG_IOSCHED1=y
    CONFIG_CGROUP1=y
ddd(depends: )
    CONFIG_GENERIC_CPU1=y
    CONFIG_CRYPTO_AES_NI1=m

to

1. a(depends: )
2. bb(depends:lmbench )
3. cc(depends: )
    CONFIG_DEFAULT1=y
    CONFIG_IOSCHED1=y
    CONFIG_CGROUP1=y

4. ddd(depends: )
    CONFIG_GENERIC_CPU1=y
    CONFIG_CRYPTO_AES_NI1=m


With over three hundred posts to your credit, we would hope that you would have a good idea by now on how to do something like this. What have you tried to solve this problem?

What operating system are you using?

What shell are you using?

3 Likes

I'm using bash or sh shell on ubuntu

1 Like

Meat Loaf may say Two Out of Three Ain't Bad, but it's not sufficient here. I repeat: What have you tried to solve this problem?

And, what in your description of this problem explains why the input line:

    CONFIG_GENERIC_CPU1=y

is supposed to be transformed to:

    CONFIG_GENERIC_CPU_AUTO1=y

in the output???

sorry, my typo, I just updated it

---------- Post updated at 01:54 AM ---------- Previous update was at 01:28 AM ----------

I tried awk '{print NR, "\t", $0}' , but it adds each line with number , it's not my case

Your specification used a period and a space after the sequence number (which is not an input line number so the awk NR variable won't work), not a tab. Try:

awk '!/^ /{printf("%d. %s\n",++n,$0);next}1' file

But after "correcting" the output you said you wanted before, you have now added an empty line in the middle of the output that the above code will not produce. (And I see no obvious reason why it should be there???)

Dear yanglei_fage,

We have questions needing a response first:-

  • What have you tried so far?
  • What output/errors do you get?
  • What are your preferred tools beyond the shell if needed? (C, sed, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)

Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.

We're all here to learn and getting the relevant information will help us all.

Robin

Hi.

Standard utility nl could be used:

#!/usr/bin/env bash

# @(#) s1       Demonstrate numbering lines matching regular expression, nl.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C nl

FILE=${1-data1}
E=expected-output.txt

pl " Input data file $FILE:"
cat $FILE

pl " Expected output:"
cat $E

pl " Results:"
nl -bp"^[a-zA-Z]" -nln -s". " -w 1 $FILE

exit 0

producing:

$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.4 (jessie) 
bash GNU bash 4.3.30
nl (GNU coreutils) 8.23

-----
 Input data file data1:
a(depends: )
bb(depends:lmbench )
cc(depends: )
    CONFIG_DEFAULT1=y
    CONFIG_IOSCHED1=y
    CONFIG_CGROUP1=y
ddd(depends: )
    CONFIG_GENERIC_CPU1=y
    CONFIG_CRYPTO_AES_NI1=m

-----
 Expected output:
1. a(depends: )
2. bb(depends:lmbench )
3. cc(depends: )
    CONFIG_DEFAULT1=y
    CONFIG_IOSCHED1=y
    CONFIG_CGROUP1=y

4. ddd(depends: )
    CONFIG_GENERIC_CPU1=y
    CONFIG_CRYPTO_AES_NI1=m

-----
 Results:
1. a(depends: )
2. bb(depends:lmbench )
3. cc(depends: )
       CONFIG_DEFAULT1=y
       CONFIG_IOSCHED1=y
       CONFIG_CGROUP1=y
4. ddd(depends: )
       CONFIG_GENERIC_CPU1=y
       CONFIG_CRYPTO_AES_NI1=m

The sample input is small, so some adjustments may be necessary for production input.

See man nl for details.

Best wishes ... cheers, drl