Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Logic to separate the first name in the file

Shell Programming and Scripting


Tags
shell, shellscript, shellscripting, shellscripts

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-13-2017   -   Original Discussion by ricky-row
ricky-row ricky-row is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 15 November 2017, 4:15 AM EST
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
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.
Sponsored Links
    #2  
Old Unix and Linux 06-13-2017   -   Original Discussion by ricky-row
Aia's Unix or Linux Image
Aia Aia is offline
Registered User
 
Join Date: May 2008
Last Activity: 20 November 2017, 9:27 AM EST
Posts: 1,673
Thanks: 49
Thanked 641 Times in 601 Posts
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?
Sponsored Links
    #3  
Old Unix and Linux 06-13-2017   -   Original Discussion by ricky-row
ricky-row ricky-row is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 15 November 2017, 4:15 AM EST
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
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 Unix and Linux 06-13-2017   -   Original Discussion by ricky-row
Aia's Unix or Linux Image
Aia Aia is offline
Registered User
 
Join Date: May 2008
Last Activity: 20 November 2017, 9:27 AM EST
Posts: 1,673
Thanks: 49
Thanked 641 Times in 601 Posts

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

Sponsored Links
    #5  
Old Unix and Linux 06-13-2017   -   Original Discussion by ricky-row
ricky-row ricky-row is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 15 November 2017, 4:15 AM EST
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #6  
Old Unix and Linux 06-13-2017   -   Original Discussion by ricky-row
Aia's Unix or Linux Image
Aia Aia is offline
Registered User
 
Join Date: May 2008
Last Activity: 20 November 2017, 9:27 AM EST
Posts: 1,673
Thanks: 49
Thanked 641 Times in 601 Posts
Please, amend the following part.
"${f%%-[0-9]*}"
Sponsored Links
    #7  
Old Unix and Linux 06-20-2017   -   Original Discussion by ricky-row
ricky-row ricky-row is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 15 November 2017, 4:15 AM EST
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
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:
Logic to separate the first name in the file
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
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes



All times are GMT -4. The time now is 08:44 AM.