LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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, 10:37 AM   #1
Drigo
Member
 
Registered: Jun 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, 01:48 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

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

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

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

Rep: Reputation: 103Reputation: 103
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 07:34 PM.
 
Old 10-24-2012, 07:40 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

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

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
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 01:52 AM
sed code to replace character vishesh Linux - Newbie 2 05-17-2011 09:01 AM
How to replace last character string nicklau81 Programming 10 01-21-2011 04:28 AM
Perl: replace character in file vippie Programming 4 03-23-2007 05:26 AM


All times are GMT -5. The time now is 10:48 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration