1

How can I print variables to an output file in two or more columns (using echo for example). I have the following:

grep -oP 'value\s*=\s*\K.*' file >x_vs_y  # x values

for X in $(seq 0 50 400)
do
   echo  "$X"  >>x_vs_y    # y values
done

Output: (x_vs_y)

1.08759432363
1.08477216702
1.08211988431
1.07947977045
1.07685185457
1.07423631941
1.07163282502
1.06904156798
1.06646245052
0
50
100
150
200
250
300
350
400

With this script I get an output file in a single column (18x1), but I want to get a 9x2 array (X vs Y) like:

Output: (x_vs_y)

1.08759432363 0
1.08477216702 50
1.08211988431 100
1.07947977045 150
1.07685185457 200
1.07423631941 250
1.07163282502 300
1.06904156798 350
1.06646245052 400
1
  • Is the $(seq just an example or the actual requirement? This is important to know to select the right tool. Maybe read man paste if this is what you are looking for.
    – Philippos
    May 29, 2019 at 7:02

2 Answers 2

0

Consider this input file:

$ cat file
value = 1.08759432363
value = 1.08477216702
value = 1.08211988431
value = 1.07947977045
value = 1.07685185457
value = 1.07423631941
value = 1.07163282502
value = 1.06904156798
value = 1.06646245052

To print two columns:

$ awk '{print x+0, $3; x+=50}' file
0 1.08759432363
50 1.08477216702
100 1.08211988431
150 1.07947977045
200 1.07685185457
250 1.07423631941
300 1.07163282502
350 1.06904156798
400 1.06646245052

Saving the output in a file

$ awk '0==a[$0]++' file >outfile
$ cat outfile
value = 1.08759432363
value = 1.08477216702
value = 1.08211988431
value = 1.07947977045
value = 1.07685185457
value = 1.07423631941
value = 1.07163282502
value = 1.06904156798
value = 1.06646245052
2
  • Thanks, it worked, but using awk, how can I export the obtained array to an output file?
    – Yro
    May 29, 2019 at 6:56
  • 1
    @Yro redirect it: awk '{...}' input-file > output-file
    – muru
    May 29, 2019 at 7:00
0

Maybe a combination of paste and seq can be simpler:

paste datafile <( seq 0 50 $(( ($(wc -l datafile | cut -d' ' -f1 )-1)*50 )) )  > output

The last value for seq is just based on the line numbers in your input datafile.

You must log in to answer this question.

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