Logic to separate the first name in the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Logic to separate the first name in the file
# 1  
Old 06-13-2017
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.
# 2  
Old 06-13-2017
Let's start first doing a test.

Issue the following command while in the directory you want:

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

Is the result what you hope?
# 3  
Old 06-13-2017
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
# 4  
Old 06-13-2017
Code:
for f in *.rpm; do echo "${f%-[0-9]*}"; done

# 5  
Old 06-13-2017
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

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

this code works for me fine

Last edited by rbatte1; 06-13-2017 at 01:01 PM.. Reason: Added CODE tags
# 6  
Old 06-13-2017
Please, amend the following part.
"${f%%-[0-9]*}"
# 7  
Old 06-20-2017
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
Code:
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.

Code:
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


Moderator's Comments:
Mod Comment
Please wrap all code, files, input & output/errors in CODE tags.
it makes it easier to read and preserves spaces for indenting or fixed-width data.

Last edited by rbatte1; 06-20-2017 at 10:47 AM.. Reason: Added CODE tags
Login or Register to Ask a Question

Previous Thread | Next Thread

1 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies
Login or Register to Ask a Question