简体   繁体   中英

How to read a file and write in to another file using a shell script

I have a file which looks like this (file1.txt)

258.2222

I have to write this file1.txt value to another file. if there in no value in file1.txt then it should print as "Passed".

this is what I tried

for final in $(cat file1.txt);do
if [ "$final" ];then
        echo $final  > file2.txt
else
        echo "Passed" > file2.txt
fi
done

this only works with 1 scenario. if there is no value in file1.txt then it is not writing as "Passed"

expected output:

if there is a value in file1.txt :

258.2222

if there is no value (empty) in file1.txt :

Passed

Can someone help me to figure out this? Thanks in advance!

Note: I am not allowed to use general purpose scripting language (JavaScript, Python etc).

The line for final in $(cat file1.txt); do for final in $(cat file1.txt); do is always of questionable value. It will munge whitespace, and is generally a bad idea. Sometimes it has a place, but for the most part there are better ways to iterate over the contents of a file. In your case, this is causing an issue because when the file is empty the loop is executed zero times.

It's not clear to me if you are trying to parse the value in some way, or filter it from other content. If you just want to copy the full contents of the file or write "Passed" if the file is empty, you can do:

if test -s file1.txt; then
    cat file1.txt
else
    echo Passed
fi > file2.txt

or

if ! grep '[^ ]' file1.txt; then echo Passed; fi > file2.txt

The test command in the first case will succeed (return 0) if file1.txt exists and has some content. Note that this may simply be whitespace; if the file having non-zero size is not sufficient for your needs, you may want to use the second solution where grep only succeeds if it matches at least one line that has a non-space character (which may be a tab.). (But note that this grep will filter out lines that are only space!). If the test or the grep returns non-zero (eg, the file is empty or there are no lines that contain non-whitespace), then "Passed" is written. Either way, the output is redirected to file2.txt .

You can just this 2 line code in bash :

# get file content in variable s
s="$(<file)"

# set s to Passed if s is empty/unset
s="${s:-Passed}"

You can use file to check if the file is empty

if [[ $(file file1.txt) =~ "empty" ]]; then 
    echo "Passed" 
else 
    cat file1.txt > file2.txt
fi

that is mean file1.txt is empty

Then I would harness GNU AWK for this task following way

awk 'END{print NR?$0:"Passed"}' file1.txt > file2.txt

Explanation: I use so called ternary operator condition ? valueiftrue : valueiffalse for condition I use just NR (number row) which inside END is total number of rows processed, if file1.txt has one or more rows I print $0 which inside END is last line of file, otherwise ie where there is 0 rows in file I print text Passed .

(tested in gawk 4.2.1)

If file1.txt contains something (its file size is non-zero), you want that something to go into file2.txt :

if [ -s file1.txt ]; then
    cp file1.txt file2.txt
fi

If file1.txt is empty (or missing), then you want a line saying Passed written to file2.txt :

if [ -s file1.txt ]; then
    cp file1.txt file2.txt
else
    echo Passed >file2.txt
fi

If you want to avoid creating file2.txt if file1.txt is missing:

if [ -s file1.txt ]; then
    cp file1.txt file2.txt
elif [ -e file1.txt ]; then
    echo Passed >file2.txt
fi

The echo is now only executed if the -s test fails (the file does not exist, or it is empty) and -e test succeeds (the file exists).

At no point do you actually have to read the data from the file in a loop.

Using any awk:

awk '{print} END{if (!NR) print "Passed"}' file1.txt > file2.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM