Example:
file1
Speed: 50.00 Temperature: 120.00
Speed: 51.00 Temperature: 121.00
Speed: 52.00 Temperature: 122.00
file2
50.00 120.00
51.00 121.00
52.00 122.00
I want to write file1 to file2
Example:
file1
Speed: 50.00 Temperature: 120.00
Speed: 51.00 Temperature: 121.00
Speed: 52.00 Temperature: 122.00
file2
50.00 120.00
51.00 121.00
52.00 122.00
I want to write file1 to file2
Assuming the fields are separated by a single space:
cut -d" " -f2,4 file1 > file2
The awk
solution is probably the shortest and concise and probably faster on large files but the shell can do that also. Here is one way.
Using bash.
while read -ra line; do
printf '%s %s\n' "${line[1]}" "${line[3]}"
done < file1 > file2
Bash has -a
option for the builtin read which creates an array per line and the while loop will take care of the lines in the file. The only advantage of this solution is that it does not use any external command from the shell.
A more portable solution would require a lot PEs.
read -a
is not standard (does not work in most other shells). Not that it matters a great deal though... – Kusalananda Jan 16 '20 at 14:58-a
option is non-standard. Try running it indash
, for example, or even bash called assh
. POSIX shells don't support arrays at all. – terdon Jan 16 '20 at 17:00