LINUX.ORG.RU

Редактирование файла в shell

 , , ,


1

1

Народ подскажите пожалуйста как решить такую задачу необходимо отредактировать текстовый файл file.txt, содержащий внутри себя название файлов:

file1.txt

file2.txt

………

fileN.txt

так, чтобы перед именами фалов стояли команды вот так:

command1|command2 file1.txt

command1|command2 file2.txt

………………………

command1|command2 fileN.txt



Последнее исправление: xxoaea (всего исправлений: 3)

#!/bin/bash

# Check if a file name has been provided as an argument
if [ $# -ne 1 ]; then
  echo "Error: please provide the name of the file as an argument."
  exit 1
fi

# Get the file name from the argument
file_name=$1

# Loop through each line of the file
while IFS= read -r line; do
  # Skip empty lines
  if [ -z "$line" ]; then
    continue
  fi

  # Add the prefix "command1|command2" to the line
  line="command1|command2 $line"

  # Print the formatted line
  echo "$line"
done < "$file_name"

Результат

kambulya999
()