4
#! /bin/bash
for (( l = 1 ; l <= 50; ++l )) ; do
       for (( k = 1 ; k <= 1000; ++k )) ; do
       sed -n '$l,$lp' $k.dat >> ~/Escritorio/$l.txt
       done
done

The script is located in a folder together with 1000 dat files each one having 50 lines of text.

The dat files are called 1.dat, 2.dat,...., 1000.dat

My purpose is to make files l.txt, where l.txt has the l line of 1.dat, the l line of 2.dat, etc. For that, I use the sed command to select the l file of each dat file.

But when I run the above script, the txt created have nothing inside...

Where is the mistake?

3
  • 2
    I don't think the loop(s) are the problem - did you actually try something simple like l=2; sed -n '$l,$lp' somefile? Remember that shell variables are not expanded when inside single quotes. Also $lp would refer to a variable called lp (you probably want sed -n "${l}p" instead) Nov 2, 2016 at 1:22
  • ive tried one single loop, using for example l=5 and it actually works.
    – Diego
    Nov 2, 2016 at 1:35
  • variables don't expand inside single quotes.
    – Dalvenjia
    Nov 2, 2016 at 1:54

2 Answers 2

6
for LINE in {1..50}; do
    for FILE in {1..1000}; do
        sed -n "${LINE}p" "${FILE}.dat" >>"~/Escritorio/${LINE}.dat"
    done
done

In your script you are using single quotes for the sed expression, variables don't expand inside single quotes, you need to use double quotes.

Also there is a one liner with awk that can do the same:

awk 'FNR<=50 {filename=sprintf("results/%d.dat", FNR); print >> filename; close(filename)}' *.dat

Just create the results directory, or change it in the command to another one, ~ does not expand to home there.

2
  • Concur you can't singlequote sed command using shell var, but in this case you don't need to quote at all and you only need line number once: ${LINE}p. OTOH you do need -n (as Q had). OT3H agree awk is better. Nov 2, 2016 at 12:27
  • @dave_thompson_085: Thanks for the observations, answer edited.
    – Dalvenjia
    Nov 2, 2016 at 16:30
4

how about this awk command ? try with small set of dat files

awk '{file=FNR".txt"; print $0 > file}' *.dat
1
  • note $0 is not necessary (it is the default for print), so print > file suffices.
    – fedorqui
    Nov 2, 2016 at 9:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .