Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 07-04-2012, 07:01 AM   #1
Registered: Sep 2011
Location: Philly
Distribution: Gentoo
Posts: 473

Rep: Reputation: Disabled
bash scripting and sed

I'm reading a book on sed/awk now but I would like to be done with this sometime today. I have 2 files
one with hundreds of <LF> delineated urls "file1"
all of the urls are of the format:
Code: feature=player_detailpage&v=xxxxxxx
I have a file full of hundreds of <LF> delineated file names. "file2"

I have a directory full of mpg files, all of them have names corresponding to the characters after the last equals sign.

all these files are in the same directory

I want to write a script to 1

1. use sed to parse file 1 and remove everything prior to and including the equals sign, and save this to "file3"

2. write a rename script to parse "file1",
lets say the first line is F93jfs,
I want it to attempt to rename F93jfs.mpg to the first line of "file2" followed by mpg.

Last, Just so everyone knows yes I am trying to batch rename many streamed files. All which I have the rights to. Nothing illegal.

second. I'm not just asking someone to write code for me, I've been fiddling with sed all morning and I'm not having a lot of luck.

Thanks in advance.
Old 07-04-2012, 07:35 AM   #2
Registered: Jan 2012
Location: South Africa
Posts: 498

Rep: Reputation: 188Reputation: 188
Hi there,

Here's a few quick commands that may help.

Firstly, to remove everything up to the equal sign in file1, try:
sed -e 's/^.*=//g' file1 > file3
To create the "rename" script, try:
paste file2 file3 > file4
sed -e 's/^/mv /' -e 's/$/.mpg/' file4 > script1
bash script1
I hope this helps!
Old 07-04-2012, 07:46 AM   #3
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
One step at a time----to parse a file and "remove everything prior to and including the equals sign", try this:
sed 's/^[^=]*=//' filename > newfilename
At the beginning of the line, find any number of characters not "=", followed by "="; and replace with nothing.

The generic form:
sed 's/stufftofind/replacement/'
----the delimiter is "/", but any character can be used---the delimiter is selected such that it does not also appear in the search or replacement strings.

Last edited by pixellany; 07-04-2012 at 07:48 AM.
Old 07-04-2012, 08:28 AM   #4
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,483

Rep: Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864
Or with awk you could try something like:
awk -F= 'NR==FNR{f[++i]=$NF".mpg";next}/.mpg$/ && ++j < i{print | "mv " f[j] " " $1}' file1 file2
Old 07-04-2012, 06:43 PM   #5
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,347

Rep: Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365
You may find the sed & awk howtos here useful


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Scripting using a Mix of Bash, Awk, and Sed rafir Programming 5 10-12-2011 01:59 PM
Bash scripting - sed, lame, etc. Jinouchi Linux - Newbie 10 03-22-2010 05:03 AM
bash scripting with sed not working fakie_flip Linux - Software 6 12-08-2009 01:56 AM
scripting help with sed n0gnus Linux - General 4 09-30-2008 11:01 AM
Simple bash/awk/sed scripting question R00ts Programming 4 04-16-2005 02:55 AM

All times are GMT -5. The time now is 02:59 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration