1

Solved

Used cut -c 4- in code and added new variable CURFILE1 which is used to print file "$CURFILE1" {

CURFILE="$(basename "$file")"
CURFILE1=$(echo $CURFILE | cut -c 4-)**

Code part before

cp -a $AUTOFILE $AUTOFILE.old > $AUTOFILE.new
for file in /../pictures/*; do
    CURFILE="$(basename "$file")"
    cat <<EOF >> $AUTOFILE.new
file "$CURFILE" {

Code part after changes

cp -a $AUTOFILE $AUTOFILE.old > $AUTOFILE.new
for file in /../pictures/*; do
    CURFILE="$(basename "$file")"
    CURFILE1=$(echo $CURFILE | cut -c 4-)**
    cat <<EOF >> $AUTOFILE.new
file "$CURFILE1" {

Hello, can someone help me with bash script? I am working to create auto fill file with information about existing files in directory with some params and more code on it...

Problem is that all files are using prefix ex.: f1.file, so I need to remove f1. and leave only file. Information about found files is stored as information "Text" in one single file.

I need to use some function in bash when files are generating and auto remove prefix in this line file "$CURFILE" {. CURFILE is the name as example: f1.file and I need to remove that f1.

Just to be more concrete:

  1. Bash generates text file from all files in directory with custom parameters from code.
  2. Problem is that text is generated with prefix f1,f2,f3.... because file names is f1.picture f2.test etc..
  3. I need to show in data file only string without f1,f2,f3... etc..
  4. Can someone help me to modify this line with some function? (Seed etc I know there is, but I don't know how to addapt it to existing line. Thank you!
Toto
  • 89,455
  • 62
  • 89
  • 125
zte813
  • 95
  • 1
  • 10
  • 1
    [parameter expansion](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html) – jhnc Jul 21 '21 at 18:40
  • 1
    https://stackoverflow.com/questions/965053/extract-filename-and-extension-in-bash ? – KamilCuk Jul 21 '21 at 18:58

2 Answers2

3

Please don't post fragments of code.

See How to create a Minimal Reproducible Example.

Let me rephrase your question in the way we use to do it at SO:

You have a variable CURFILE containing the string f1.file and you want it to contain file instead. Just do:

CURFILE=${CURFILE#*.}

Assuming the prefix you have to remove is delimited by a ., ${CURFILE#*.} will remove any string followed by a dot from the front of your string.

Pierre François
  • 5,850
  • 1
  • 17
  • 38
  • Hello @Pierre, thanks for help, but strange thing is that it doesn't delete needed part, also I found many files that has this full name: f1.pic.s3 so I need only to delete f1. part, but first time doing something with bash, so every help from anyone is appreciated. :( – zte813 Jul 21 '21 at 20:33
  • 1
    @zte813 Why did you accept the answer as correct if it did not solve your problem? Instead edit your question and clarify. – Gerhard Jul 21 '21 at 22:05
  • 1
    If the string in `CURFILE` contains `f1.pic.s3`, `${CURFILE#*.}` will contain `pic.s3`, but `${CURFILE##*.}` will contain `s3`. The operator `#` deletes the shortest matching string `*.`, `##` matches the longest one. – Pierre François Jul 22 '21 at 08:40
  • @Gerhard both solutions can work and is working, but used more simple option with cut. – zte813 Jul 23 '21 at 12:23
1

According your code, you always want to create a new variable CURFILE1, which is identical to CURFILE, but without the first 3 characters. It is unneccessary to use command substitution with child processes for this. Just do a

CURFILE1=${CURFILE#???}

If you want to ensure that only a f1. is removed, and not, say, an initial f2., to instead a

CURFILE1=${CURFILE#f1.}
user1934428
  • 19,864
  • 7
  • 42
  • 87