0

I have a file Sample_20210805_009.txt file with data-

 ID|Date|Marks|Age|Status|ContactNo
 1|11:25:08|56.76|22|Pass|8787

So I want to develop a script run.sh which will create an output file with name "output.20210805.class.txt" (So the name of output file should be taken from sample files date i.e., Sample_20210805_009.txt - "20210805". And output in that file should be-

 NAME SET <GAIN A> D5.MARKS[05AUG2021] = 56.76
 NAME SET <GAIN A> D5.AGE[05AUG2021] = 22
 NAME SET <GAIN A> D5.STATUS[05AUG2021] = PASS
 NAME SET <GAIN A> D5.CONTACTNO[05AUG2021] = 8787
 NAME SET <GAIN A> D5.CLASS[05AUG2021] = "10"

In this output file the last line and the content upto D5. would same. Also the date generating after each object should be automatic on the basis of given file name i.e., 20210805 so date is [05AUG2021]. Can anyone please help?

Toto
  • 89,455
  • 62
  • 89
  • 125
Saurabh
  • 1
  • 3
  • 1
    Hello, welcome on SO. What did you try? – Renaud Pacalet Aug 05 '21 at 12:06
  • 1
    consider reviewing [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and then come back and update your question accordingly; in particular, provide the code you've tried and the (wrong) output generated by your script; also, last line of your desired output has a value of `10` but there's no indication of how is this determined; also, what happens if you have multiple files with the same date but different suffixes (eg, `007`, `008`, `009`)? – markp-fuso Aug 05 '21 at 12:11
  • Thanks for suggestion @markp-fuso the last line is constant so "10" will not change we can just print it. And the suffix in input file not important it may change may not! But before suffix there is date which is important. Also I tried it by Sed command and saving it to different file but I am not able to modify data while writing to another file. Please if anyone have answer, paste whole script as I am new to shell scripting! – Saurabh Aug 05 '21 at 12:22
  • 2
    The usual work flow with `sed` is to modify data while writing to another file, so it's not clear why that's not working for you. Show what you've tried. Note that `sed` is almost certainly the wrong tool. Look to `awk`. – William Pursell Aug 05 '21 at 14:17
  • 2
    another question ... sample only shows one line of 'data' ... what do you expect if the input contains multiple lines of data? generate a separate batch of `NAME SET` strings, or maintain a single batch of `NAME SET` strings but with (comma) delimited values on the end of the string? – markp-fuso Aug 05 '21 at 15:07
  • Look for "transpose csv" and decide which tool will you use. See https://stackoverflow.com/questions/1729824/an-efficient-way-to-transpose-a-file-in-bash – lojza Aug 05 '21 at 18:13

1 Answers1

0

If ed is available/acceptable with the help of the shell and some Linux/Unix utilities.

#!/bin/sh

file=Sample_20210805_009.txt
temp=${file%_*}
last_entry='CLASS "10"'
date_to_convert=${temp#*_}
converted_date=$(date -d"$date_to_convert" '+%d%b%Y')

ed -s "$file" <<EOF
H
%s/^[^|]*|[^|]*|//
w tmpa
%d
0a
$last_entry
.
0r !tr '|' '\n' < tmpa | tr '[:lower:]' '[:upper:]' | pr -t2s' '
%s/^\(.*\) \(.*\)/NAME SET <GAIN A> D5\.\1[$converted_date] = \2/
%p
!rm tmpa
Q
EOF

  • The H should show some error message and not just a plain ? if and when ed should encounter an error.

  • GNU date(1) is used for converting the date format.

  • It creates a temp file named tmpa at the line where w tmpa is written, which is also deleted at the line where !rm tmpa is written.

  • Change Q to w if in-place editing is needed.

  • To save the result/output in a new file, change Q to w output.txt or in your case w output.$converted_date.class.txt

  • Remove the line where %p is at to silence the output.

  • If tmpa is not good enough for whatever reason, see How do I create a temporary file in a secure manner?

Jetchisel
  • 7,493
  • 2
  • 19
  • 18
  • Thank you so much @Jetchisel for answering, I tried to run this script but for some reason it is returning only "?". Do you have any thoughts what must be going wrong? Also I want to store the output in separate file output.20210805.class.txt with same date as mentioned in date in input file, also it is creating tmpa file only and store the data in that file, please help me out if u can! Thanks! – Saurabh Aug 07 '21 at 10:32
  • Ok, put a `H` after the line `ed -s "$file" < – Jetchisel Aug 07 '21 at 10:46
  • Still the same output "?" – Saurabh Aug 07 '21 at 16:15
  • I have tested that code in Cygwin, Linux, and FreeBSD and it works as expected, in FreeBSD I have not installed GNU `date`(1) so that is the only error Ive got in there but the rest are fine. Not sure what you're doing wrong. – Jetchisel Aug 08 '21 at 16:12
  • Thanks @Jetchisel for helping me out! I have resolved the issue! – Saurabh Aug 10 '21 at 04:03