Skip to main content
spelling, clarity of headline; syntax-color code block
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

I am new to Unix scrptingscripting.

I want to rename a file based on a matching string in the first line (i.e. line starting with HDRHDR).

I have a text file (File.txtFile.txt) which looks like below:

HDR##############################1234###
########################################
########################################
HDR##############################1234###
########################################
########################################

Below is my code,. How can I be more specific in my code to check only the first line in the file, which startingstarts with HDRHDR and havinghas the pattern 12341234 or 5678.5678?

    if grep -o "1234" File.txt
    
    then mv File.txt Pattern1.txt
    
    echo "File with pattern1 received..."
    
    elif grep -o "5678" File.txt
    
    then mv File.txt Pattern2.txt
    
    echo "File with pattern2 received..."
    
    else
    
    echo "File have no matching pattern..."
    
    fi

I am new to Unix scrpting

I want to rename a file based on a matching string in the first line (i.e. line starting with HDR)

I have a text file (File.txt) which looks like below:

HDR##############################1234###
########################################
########################################

Below is my code, How can I be more specific in my code to check only the first line in the file which starting with HDR and having pattern 1234 or 5678.

    if grep -o "1234" File.txt
    
    then mv File.txt Pattern1.txt
    
    echo "File with pattern1 received..."
    
    elif grep -o "5678" File.txt
    
    then mv File.txt Pattern2.txt
    
    echo "File with pattern2 received..."
    
    else
    
    echo "File have no matching pattern..."
    
    fi

I am new to Unix scripting.

I want to rename a file based on a matching string in the first line (i.e. line starting with HDR).

I have a text file (File.txt) which looks like below:

HDR##############################1234###
########################################
########################################

Below is my code. How can I be more specific in my code to check only the first line in the file, which starts with HDR and has the pattern 1234 or 5678?

if grep -o "1234" File.txt

then mv File.txt Pattern1.txt

echo "File with pattern1 received..."

elif grep -o "5678" File.txt

then mv File.txt Pattern2.txt

echo "File with pattern2 received..."

else

echo "File have no matching pattern..."

fi

Renaming a text file based on matching string in fistfile's first line of the file, in Unix shell script

I am new to Unix scrpting

I want to rename a file based on a matching string in the first line (i.e. line starting with HDR)

I have a text file (File.txt) which looks like below:

HDR##############################1234###
########################################
########################################

Below is my code, How can I be more specific in my code to check only the first line in the file which starting with HDR and having pattern 1234 or 5678.

if grep -o "1234" File.txt

then mv File.txt Pattern1.txt

echo "File with pattern1 received..."

elif grep -o "5678" File.txt

then mv File.txt Pattern2.txt

echo "File with pattern2 received..."

else

echo "File have no matching pattern..."

fi
    if grep -o "1234" File.txt
    
    then mv File.txt Pattern1.txt
    
    echo "File with pattern1 received..."
    
    elif grep -o "5678" File.txt
    
    then mv File.txt Pattern2.txt
    
    echo "File with pattern2 received..."
    
    else
    
    echo "File have no matching pattern..."
    
    fi

Renaming a text file based on matching string in fist line of the file in Unix shell script

I am new to Unix scrpting

I want to rename a file based on a matching string in the first line (i.e. line starting with HDR)

I have a text file (File.txt) which looks like below:

HDR##############################1234###
########################################
########################################

Below is my code, How can I be more specific in my code to check only the first line in the file which starting with HDR and having pattern 1234 or 5678.

if grep -o "1234" File.txt

then mv File.txt Pattern1.txt

echo "File with pattern1 received..."

elif grep -o "5678" File.txt

then mv File.txt Pattern2.txt

echo "File with pattern2 received..."

else

echo "File have no matching pattern..."

fi

Renaming a text file based on matching string in file's first line, in Unix shell script

I am new to Unix scrpting

I want to rename a file based on a matching string in the first line (i.e. line starting with HDR)

I have a text file (File.txt) which looks like below:

HDR##############################1234###
########################################
########################################

Below is my code, How can I be more specific in my code to check only the first line in the file which starting with HDR and having pattern 1234 or 5678.

    if grep -o "1234" File.txt
    
    then mv File.txt Pattern1.txt
    
    echo "File with pattern1 received..."
    
    elif grep -o "5678" File.txt
    
    then mv File.txt Pattern2.txt
    
    echo "File with pattern2 received..."
    
    else
    
    echo "File have no matching pattern..."
    
    fi
Source Link

Renaming a text file based on matching string in fist line of the file in Unix shell script

I am new to Unix scrpting

I want to rename a file based on a matching string in the first line (i.e. line starting with HDR)

I have a text file (File.txt) which looks like below:

HDR##############################1234###
########################################
########################################

Below is my code, How can I be more specific in my code to check only the first line in the file which starting with HDR and having pattern 1234 or 5678.

if grep -o "1234" File.txt

then mv File.txt Pattern1.txt

echo "File with pattern1 received..."

elif grep -o "5678" File.txt

then mv File.txt Pattern2.txt

echo "File with pattern2 received..."

else

echo "File have no matching pattern..."

fi