Help for "sed" command - need your help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help for "sed" command - need your help
# 1  
Old 06-29-2018
Help for "sed" command - need your help

Hi Friends
Need you help on this.
i used below line in my script for capturing some of the file names
Code:
ls -1 $filepath|sed 's/[^0-9]*\([0-9]\{8\}\).*/\1/g'|uniq|awk -vEX=$filepath '{print "zip -umD "$0".zip *"$0"*" }'| sh -s || true;

sample file name is PP05000120180627.134
and it selects -> 05000120.zip
how do i get selected this part of file name -- > 20180627.zip

Last edited by Scrutinizer; 06-29-2018 at 04:31 AM.. Reason: Code tags
# 2  
Old 06-29-2018
Try:
Code:
sed -n 's/.*\([0-9]\{8\}\)\..*/\1/p'

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 06-29-2018
How about a single awk script?
Code:
ls | awk '
match ($0, /[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\./)        {TMP[substr ($0, RSTART, RLENGTH-1)]++
                                                                }
END                                                             {for (t in TMP) print "zip -umD " t ".zip *" t "*" 
                                                                }
'
zip -umD 20180627.zip *20180627*

and pipe into sh. The looong regex is due to my mawk doesn't allow for bounds; if yours does, try [0-9]{8} as you did before.
This User Gave Thanks to RudiC For This Post:
# 4  
Old 06-29-2018
Quote:
Originally Posted by RudiC
How about a single awk script?
How about no script at all, only pure shell :-) :

Code:
ls -1 | while read FILE ; do
     FILE="${FILE#PP??????}"
     FILE="${FILE%.*}.zip"
     do_something "$FILE"
done

I hope this helps.

bakunin
# 5  
Old 06-29-2018
The requestor needs to tell files matching his pattern from others, and remove duplicates.


EDIT: Mayhap like so:
Code:
ls *[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].* |
while read FILE
  do    FILE="${FILE%.*}"
        TMP="${FILE#${FILE%????????}}"
        [ ${ARR[$TMP]} ] || echo zip -umD ${TMP}.zip *${TMP}*
        ARR[$TMP]=1
  done


Last edited by RudiC; 06-29-2018 at 06:58 AM..
This User Gave Thanks to RudiC For This Post:
# 6  
Old 06-29-2018
Hi friend
Thanks for input
..
now i am using below expression
Code:
ls -1 $EX|sed 's/\([0-9]\{8\}\)\..*/\1/g'|uniq|awk -vEX=$EX '{print "zip -umD "$0".zip *"$0EX"*"}'|sh -s || true;

my file list as below
EJ05000120180627.134


but it finds .zip file. how do i remove that
Code:
zip warning: EJ05000120180627.zip not found or empty
  zip warning: name not matched:

Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 06-29-2018 at 09:25 AM.. Reason: Added CODE tags.
# 7  
Old 06-29-2018
Note:
ls -1 is unnecessary when feeding a pipe, since the output is not to a terminal . From man ls:
Code:
    -1      (The numeric digit ``one''.)  Force output to be one entry per line.  This is the default when output is not to a termi-
             nal.

So in this case ls -1 is the same as ls
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

2. Shell Programming and Scripting

Awk,sed : change every 2nd field ":" to "|"

Hi Experts, I have a string with colon delimited, want 2nd colon to be changed to a pipe. data: 101:8:43:4:72:14:41:69:85:3:137:4:3:0:4:0:9:3:0:3:12:3: I am trying with sed, but can change only 1 occurance: echo "101:8:43:4:72:14:41:69:85:3:137:4:3:0:4:0:9:3:0:3:12:3:" | sed 's/:/|/2'... (5 Replies)
Discussion started by: rveri
5 Replies

3. Shell Programming and Scripting

sed returns error "sed: -e expression #1, char 18: unterminated `s' command"

Hello All, I have something like below LDC100/rel/prod/libinactrl.a LAA2000/rel/prod/libinactrl.a I want to remove till first forward slash that is outputshould be as below rel/prod/libinactrl.a rel/prod/libinactrl.a How can I do that ??? (8 Replies)
Discussion started by: anand.shah
8 Replies

4. Post Here to Contact Site Administrators and Moderators

Suggestion: adding two new groups "sed" and "awk"

Majority of the questions are pertaining file/string parsing w.r.t sed or awk It would be nice to have these two as their own sub category under shell-programming-scripting which can avoid lot of duplicate posts. (1 Reply)
Discussion started by: jville
1 Replies

5. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

6. Shell Programming and Scripting

Using sed to find text between a "string " and character ","

Hello everyone Sorry I have to add another sed question. I am searching a log file and need only the first 2 occurances of text which comes after (note the space) "string " and before a ",". I have tried sed -n 's/.*string \(*\),.*/\1/p' filewith some, but limited success. This gives out all... (10 Replies)
Discussion started by: haggismn
10 Replies

7. UNIX for Dummies Questions & Answers

Unix "look" Command "File too large" Error Message

I am trying to find lines in a text file larger than 3 Gb that start with a given string. My command looks like this: $ look "string" "/home/patrick/filename.txt" However, this gives me the following message: "look: /home/patrick/filename.txt: File too large" So, I have two... (14 Replies)
Discussion started by: shishong
14 Replies

8. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

9. Shell Programming and Scripting

Simplify Bash Script Using "sed" Or "awk"

Input file: 2 aux003.net3.com error12 6 awn0117.net1.com error13 84 aux008 error14 29 aux001.ha.ux.isd.com error12 209 aux002.vm.ux.isd.com error34 21 alx0027.vm.net2.com error12 227 dux001.net5.com error123 22 us008.dot.net2.com error121 13 us009.net2.com error129Expected Output: 2... (4 Replies)
Discussion started by: sQew
4 Replies

10. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies
Login or Register to Ask a Question