Logic to separate the first name in the file

Hi any logic to write a shell script to go back up into the previous directory and it has "n" number of files like abc-1.0.1.rpm , xyz-3.2.1.rpm , a-bd-2.3.1.rpm etc.. with same pattern. I need the first name of it before the numeric starts (which can acts as a delimiter) i.e(-1.0.1)

Kindly share your inputs.

I have a directory called test with files abc-1.1.rpm, bdf-2.12.rpm, xz-y-1.02.rpm xyz-3.2.1.rpm , a-bd-2.3.1.rpm, etc.. let be "n" files. I am going to write a script in test/testscript.sh. here this script should come out and check for the files and give me the output to a file with abc,bdf,xz-y. the file can be n numbers.

Let's start first doing a test.

Issue the following command while in the directory you want:

for f in *.rpm; do echo "${f%-*}"; done

Is the result what you hope?

Thanks Aia, but my case some files have the alphabets after the numerical too. for ex. abc-d-1.01-test.rpm. etc..
here we can use the delimiters as -(0-9) this is my idea, could you change your given code according to this please

for f in *.rpm; do echo "${f%-[0-9]*}"; done

Its just near to the output, My idea is to make the delimiter to check from the front, because the output for thsi file is server-7.10.1-1-xyz-gbc.rpm is server-7.10.1 but I need only server, I am also trying with different style but I am just a beginner I can make use of it. could you change the code once again according to this please

---------- Post updated at 01:30 PM ---------- Previous update was at 12:29 PM ----------

I got the answer thanks, for the help Aia

for f in ../*rpm; do echo "$f" | sed 's/-[0-9].*//'; done

this code works for me fine

Please, amend the following part.
"${f%%-[0-9]*}"

I have made this function in script, I need to use the VAR in other way after the function, do you have any idea how to do that

if there are 10 files with *.rpm, I need to store the VAR values like abc, xy-z, etc.. in a new file. So, after this function I have to check for the file existence first, if not have to create a file named test.txt.
then inside the file

NAME = abc 
TYPE = txt

for all the 10 fiiles.
NOTE: if already the file is existing it can overwrite the same file but 11th file is added it should not delete the existing in the test.txt, it should be added at the end.

test()
{
  pushd packages &>/dev/null
  mkdir -p ../info
  for p in $(ls *.rpm 2>/dev/null);do
    VAR=($(echo $p| sed 's/-[0-9].*//';))
    if [ -e /info/$VAR.list} ]; then continue;fi
      rpm -qlp $p | sed -re 's/^/./' > /info/$VAR.list};;
    esac
  done
  popd &>/dev/null
}
test

Hi, any idea on this?

Do not call a function test . test is a command to check file types and compare values.

oh, sorry. test is for explanation, I have my different function, its just for explaining the scenario

maybe have the function return the value. ex:

VAR=111

func()
{
  v=222
  echo $v
}

VAR=$(func)

echo $VAR

No, sorry
The value of VAR should be the input of my question

---------- Post updated at 09:17 PM ---------- Previous update was at 09:06 PM ----------

test()
{
  pushd packages &>/dev/null
  mkdir -p ../info
  for p in $(ls *.rpm 2>/dev/null);do
    VAR=($(echo $p| sed 's/-[0-9].*//';))
    if [ -e /info/$VAR.list} ]; then continue;fi
      rpm -qlp $p | sed -re 's/^/./' > /info/$VAR.list};;
    esac
  done
  popd &>/dev/null
}
test

After this function, let there are 10 files with .rpm extension has been parsed and saved with $VAR.list .
Now the other function should take the all the 10 values of VAR i.e the function can be called inside the test() function.

that VAR value has to be written inside a file name test.txt
ex: $cat test.txt
NAME=abc
NAME=xyz......

To repeat: Do not name a function test -- ever. Name it test2 if you have to, never test. There's a real command named test which will pop up when you're not expecting it.

This is redundant:

for p in $(ls *.rpm 2>/dev/null);do

Just do:

for p in .rpm ;do

I have no idea what that esac is doing as you're not using case, that's got to be a syntax error.

I think you mean popd >/dev/null 2>/dev/null not popd &>/dev/null .

I think you mean [ -e ../info/$VAR.list ] not [ -e /info/$VAR.list} ]

How about

cat ../info/* > outputfile

Thanks for the quick reply, The corrections given by you are correct, I will make changes. But the last which you said.

cat ../info/* > outputfile

I can't understand it., where I have to implement it and how it will collect all the values of VAR

It reads all the files created and pastes them into one file.

But my requirement is to look for a file in the location and if not has to create it first, if it is available I have to append these values in the end of the file. in different format, not directly.
like
ex
after the 1st function, I will be getting output as abc.list, xyz.list, cd-ef.list, gfd.list in a directoy named info.

apart from this output I need another file, let it be the same name you mentioned as outputfile. In this output file.
If I open the file it should have.

NAME= abc
TYPE=doc
NAME=xyz
TYPE=doc
NAME=cd-ef
TYPE=doc
NAME=gfd
TYPE=doc

here TPYE can be hardcoded.

Sorry to trouble, As, I am a newbee, I am struggling a lot.