1

I have a file called TempsModel.txt which is created as follows:

ls /media/Elise/2811226E69F71131/ModelOutput/* > TempsModel.txt

so it lists all the files in that directory. These files are all compressed netcdf files so it looks like this:

/media/Elise/2811226E69F71131/ModelOutput/20091201_000000.nc.gz
/media/Elise/2811226E69F71131/ModelOutput/20091201_002023.nc.gz
/media/Elise/2811226E69F71131/ModelOutput/20091201_003009.nc.gz
/media/Elise/2811226E69F71131/ModelOutput/20091201_004020.nc.gz

I need this list to not contain the .gz. How do I remove these three characters? I tried question Delete the last character of a string using string manipulation in shell script and Remove last character from line

But how do I create a second file TempsModel2.txt where the list does not contain these last three characters?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
Jellyse
  • 223
  • 1
  • 2
  • 10
  • You could do it in one operation, e.g. `ls /media/Elise/2811226E69F71131/ModelOutput/* | sed 's/.gz$//' > TempsModel.txt` – fpmurphy Jun 27 '19 at 14:30

2 Answers2

4

It's probably not best practice to create that file using ls but given the sample input you have provided you can use

awk:

awk '{sub(/.gz$/, ""); print}' TempsModel.txt > TempsModel2.txt

sed:

sed 's/.gz$//' TempsModel.txt > TempsModel2.txt

In order to create the initial file without ever having the .gz extension you could do:

for file in /media/Elise/2811226E69F71131/ModelOutput/*; do echo "${file%.gz}"; done > TempsModel.txt

You could also do:

set -- /media/Elise/2811226E69F71131/ModelOutput/*
printf '%s\n' "${@%.gz}" > TempsModel.txt
jesse_b
  • 35,934
  • 12
  • 91
  • 140
2

If zsh is available, then similar to this Using parameter expansion to generate arguments list for mkdir -p, you could use a glob qualifier to do the suffix removal on the fly:

setopt histsubstpattern extendedglob

print -rl -- /media/Elise/2811226E69F71131/ModelOutput/*.gz(#q:s/%.gz/) > TempsModel2.txt
steeldriver
  • 78,509
  • 12
  • 109
  • 152