1

I wanted to get some information about my CPU temperatures on my Linux Server (OpenSuse Leap 15.2). So I wrote a Script which collects data every 20 seconds and writes it into a text file. Now I have removed all garbage data (like "CPU Temp" etc.) I don't need.

Now I have a file like this:

47
1400
75
3800

The first two lines are one reading of the CPU temperature in C and the fan speed in RPM, respectively. The next two lines are another reading of the same measurements.

In the end I want this structure:

47,1400
75,3800

My question is: Can a Bash script do this for me? I tried something with sed and Awk but nothing worked perfectly for me. Furthermore I want a CSV file to make a graph, but i think it isn't a problem to convert a text file into a CSV file.

oguz ismail
  • 1
  • 16
  • 47
  • 69
  • 1
    Thanks, i'm new on stackoverflow! – Planetdragon Jun 29 '21 at 13:08
  • @Planetdragon : If you have two problems (generating a CSV, generating a Graph), ask two separate questions. – user1934428 Jun 29 '21 at 13:09
  • @Planetdragon : If you post code or the content of a text file, please use fixed space formatting, as described [here](https://meta.stackoverflow.com/questions/251361/how-do-i-format-my-code-blocks). – user1934428 Jun 29 '21 at 13:10
  • @Planetdragon : If you want to discuss programming problems, you need to post some code (typically your own, failed attempt to solve your problem). – user1934428 Jun 29 '21 at 13:11
  • The first question is why your collection script doesn't write its output in the format you want. What you ask should be completely trivial and obvious a few minutes into any Unix text processing tutorial for beginners. – tripleee Jun 29 '21 at 13:14

5 Answers5

3

You could use paste

paste -d, - - < file.txt

With pr

pr -ta2s, file.txt

with ed

ed -s file.txt <<-'EOF'
  g/./s/$/,/\
  ;.+1j
  ,p
  Q
EOF
Jetchisel
  • 7,493
  • 2
  • 19
  • 18
2

You can use awk:

awk 'NR%2{printf "%s,",$0;next;}1' file.txt > file.csv
deepakchethan
  • 5,240
  • 1
  • 23
  • 33
1

Another awk:

$ awk -v OFS=, '{printf "%s%s",$0,(NR%2?OFS:ORS)}' file

Output:

47,1400
75,3800

Explained:

$ awk -v OFS=, '{        # set output field delimiter to a comma
    printf "%s%s",       # using printf to control newline in output
        $0,              # output line
        (NR%2?OFS:ORS)   # and either a comma or a newline
}' file
James Brown
  • 36,089
  • 7
  • 43
  • 59
1

Since you asked if a bash script can do this, here's a solution in pure bash. ;o]

c=0
while read -r line; do
  if (( c++ % 2 )); then
    echo "$line"
  else printf "%s," "$line"
  fi
done < file
Paul Hodges
  • 13,382
  • 1
  • 17
  • 36
  • If there's a chance the last line won't have a newline character, use `while read -r line || [[ -n "$line" ]]` to make sure you pick that up. – Paul Hodges Jun 29 '21 at 13:24
  • Thanks for your answers! But awk is Bash. Isn't? – Planetdragon Jun 29 '21 at 13:24
  • 1
    `awk` is a separate executable, also available from `sh`, `zsh`, `csh`, or any other place you can call an executable. You could run it from `perl` or `C`, etc. `awk` is great, and probably a better solution, but `bash` is the command line parser. I'm just adding this for the sake of thoroughness and a touch of humor, in the case that someone somewhere has a weird system that doesn't have `awk` installed, or maybe they are just irrationally prejudiced against it, lol – Paul Hodges Jun 29 '21 at 13:27
0

Take a look at 'paste'. This will join multiple lines of text together into a single line and should work for what you want.

echo "${DATA}"
Name
SANISGA01CI
5WWR031
P59CSADB01
CPDEV02

echo "${DATA}"|paste -sd ',' -
Name,SANISGA01CI,5WWR031,P59CSADB01,CPDEV02