2

I want to use my input filename (baseline.YYYYMM.tar) in my output filename (baseline.YYYYMM.var1.tar). I can process the input files but don't know how to pass the output filename I need to my cdo application:

#!/bin/bash

prefix="basename"
fndate=$(ls | grep tar|cut -c 10-15)
var="var1"
extension=".tar"
outputfile=$prefix $fndate $var $extension
for f in $(find . -name "*.tar" -print) ; do
cdo selname,var1 $f $outputfile
done

thanks

2
  • 1
    Can you edit your question to make it clearer what the actual problem is and how we can help you? Commented Jan 25, 2015 at 21:30
  • Don't parse ls and definitely not randomly on the output of an entire directory. If you have a file you want the info for use stat on that file specifically. Commented Jan 25, 2015 at 21:43

2 Answers 2

1

(I agree with remarks about parsing ls.) Did you forget dots?

 outputfile=$prefix $fndate $var $extension

should be

outputfile=${prefix}.${fndate}.${var}${extension}
Sign up to request clarification or add additional context in comments.

1 Comment

Thx Walter. Parsing was meant as a placeholder for a safer way to provide input.
0

I'm not 100% sure I get what you're trying to do but if I'm right, you should be able to use something like this to get your output file name from the input:

var=var1
for f in *.tar; do
    output=$(awk -v s="$var" 'BEGIN{FS=OFS="."}{print $1, $2, s, $3}' <<<"$f")
    # use "$output" however you want, e.g.
    echo "$output"
done

This uses awk to split up the input file name $f and insert the shell variable $var in the middle. <<<"$f" is bash syntax, equivalent to echo "$f" | at the start of the command.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.