LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 10-24-2012, 09:37 AM   #1
Drigo
Member
 
Registered: May 2009
Posts: 111

Rep: Reputation: 17
Replace second character instead of first


Let's say I have files like:

123_abc_as656512.txt
123_abd_as65612.txt
123_abe_as16562.5txt
123_abf_as1542.txt
123_abg_as131232.txt
123_abh_a123s12.txt

I want to replace them with

123_abc
123_abd
123_abe
123_abf
123_abg
123_abh

So essentially look for the second "_" and replace everything after that with nothing.


I was looking at something like this:
for FILE in <loop in the folder>
do
FILER=${FILE//_*} #Though this will get rid of everything after the 1st "_" character

done

any help??
 
Old 10-24-2012, 12:48 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Code:
#!/bin/bash

for FILE in *; do
     NU=$(echo $FILE | sed -r 's/([^_]*_[^_]*).*/\1/')
     mv $FILE $NU
done
 
Old 10-24-2012, 01:25 PM   #3
Drigo
Member
 
Registered: May 2009
Posts: 111

Original Poster
Rep: Reputation: 17
Thanks I'll try that
 
Old 10-24-2012, 06:33 PM   #4
jv2112
Member
 
Registered: Jan 2009
Location: New England
Distribution: Arch Linux
Posts: 719

Rep: Reputation: 106Reputation: 106
Lightbulb

Another option

Code:
#! /bin/bash

for file in ls . # This assumes you are running in the directory where the files are. 
do
cp -v "$file" ${"$file";0;7} #Copy the original to a new name.This starts from position 0 in the string and takes the first 7 characters
done                      # After confirming that is what you can want you can use mv to move vs. cp to copy.

Last edited by jv2112; 10-24-2012 at 06:34 PM.
 
Old 10-24-2012, 06:40 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,441

Rep: Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791
Another option
Code:
echo 123_abc_as656512.txt|cut -d'_' -f1,2
123_abc
 
1 members found this post helpful.
Old 10-25-2012, 09:19 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,037

Rep: Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203
Is there a reason we can't just use:
Code:
for f in *.txt
do
    mv "$f" "${f%_*}"
done
 
  


Reply


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
Find and Replace character/special character from the file MyRelam Red Hat 8 05-21-2012 12:52 AM
sed code to replace character vishesh Linux - Newbie 2 05-17-2011 08:01 AM
How to replace last character string nicklau81 Programming 10 01-21-2011 03:28 AM
Perl: replace character in file vippie Programming 4 03-23-2007 04:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:41 AM.

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