1

Given file.txt that looks like this:

line_ some text
line_ some text
line_ some text

How can I number the lines like this with Bash:

line_1 some text
line_2 some text
line_3 some text

Here is my idea so far, but it doesn't work:

#!/bin/bash
var = 1
cat ./file.txt
while read line; do           
  sed "s/line_/line_(( var++ ))/"
done < ./file.txt 

4 Answers 4

3

You can use awk:

awk '$1=$1 FNR' <file>

$1 is the first word of each record (line in this case). FNR is the input record number (line number in this case).

This command is replacing the first word of each line with the first word + line-number of each line.

5
  • Thanks for your reponse. I just tried this. But it is not correct. What should I do differently? $ awk '$1=$1,FNR' <file.txt >file2.txt; cat file2.txt line_ some text line_ oaasdfa asdf line_ asdfasdf Commented Jul 30, 2015 at 18:23
  • @ScottE: new output is file2.txt Commented Jul 30, 2015 at 18:25
  • Apprantly I cannot format these comments correctly :) Commented Jul 30, 2015 at 18:26
  • This is pretty cool, I think. But can answer to the asker's complaint here? Commented Aug 1, 2015 at 1:17
  • @ScottE Your version of the code has an extra comma. Remove that comma so that the code is identical to cuonglm's and it will work. Commented Aug 1, 2015 at 1:20
0

Here is how to do it in perl:

perl -pe 'print "line_$.  $_";'  [file(s)]

If you want to send it to a file:

perl -pe 'print "line_$.  $_";'  [input_file] > [output_file]
5
  • Thanks, that worked like a charm. One tiny correction though in case anyone else references this; there was a missing ' after $_". So it should look like this: perl -ne 'print "line_$. $_"' [input_file] > [output_file] Commented Jul 30, 2015 at 18:36
  • 1
    @ScottE, line_ some text -> line_1 line_ some text. Is it correct? Commented Jul 30, 2015 at 19:09
  • Actually, this would print an extra line_. Since you just want to add the line number after the first occurrence of an underscore, you can just use perl -ne 's/_/$&$./' input_file > output_file Commented Jul 30, 2015 at 20:21
  • 1
    @JosephR., -pe. -ne prints nothing. Commented Jul 30, 2015 at 20:25
  • @EvgenyVereshchagin You're right. My bad. Commented Jul 30, 2015 at 20:29
0

It looks to me like you're well covered here, but what the hell:

sed -et -e's/_ /\n/;P;=;D' <in | paste -'d_ ' - - -  >out

sed's not exactly a whiz in the math department, but sometimes friends help it cheat, and so it gets by.

That transforms...

line_ some text
line_ some text
line_ some text

...to...

line_1 some text
line_2 some text
line_3 some text
-1

Well, you are quite new to Shell/Bash it seems, so for educational reasons, here is a solution for you, that covers your technology of choice (http://mywiki.wooledge.org/BashFAQ/001, http://mywiki.wooledge.org/BashGuide/Parameters#Parameter_Expansion)

var=1
while read -r Line; do 
  printf "%s\n" "${Line/line_/line_$var}" >> ./file-new.txt
  let var++
done < ./file.txt

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.