LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-08-2008, 12:35 AM   #1
B-Boy
Member
 
Registered: Jan 2008
Posts: 103

Rep: Reputation: 15
remove part of a word


Hi I would like to remove part of a word but with cut i cannot put no space for the delim

Code:
cat /home/xxx/Desktop/xxx/file |grep lin|cut -d " " -f 1|cut -d"" -f 1
cut: the delimiter must be a single character
Try `cut --help' for more information.
what i am trying in the above code is for example "I search for the word linux0001 but i only want the middle character "ux0"


please help and thanks in advanced
 
Old 08-08-2008, 12:59 AM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Are you saying you just want the first 3 characters after anything that contains "lin"?

If that is all you need:

sed d -n 's/.*\lin\(...\).*/\1/p' /home/xxx/Desktop/xxx/file

Beware, you probably want to be more specific, so that things like /usr/linux/foo does not match. Clarify as much as you can the pattern that is necessary to correctly determine your needs.

Last edited by Mr. C.; 08-08-2008 at 01:00 AM.
 
Old 08-08-2008, 01:13 AM   #3
B-Boy
Member
 
Registered: Jan 2008
Posts: 103

Original Poster
Rep: Reputation: 15
ok what i am trying to do is from the word usa12312301 which in my case means usa is country and 123123 is branch and 01 wkst i just want the branch number...so far i have managed to get an output of the numbers only 12312301 but now i want to take out the last two numbers 01 so that i end up with just the branch num 123123
 
Old 08-08-2008, 01:20 AM   #4
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
You want to use regular expressions here if the parts of your string vary in length. If they are all fixed length, or have clear delimiters, you can use cut or other tools for cutting operations.

So, are you branches always 6 digits, and branch numbers 2 digits?
 
Old 08-08-2008, 01:27 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
As Mr C said, if its a fixed format you could use cut:

echo usa12312301|cut -c4-9
123123
 
Old 08-08-2008, 01:28 AM   #6
B-Boy
Member
 
Registered: Jan 2008
Posts: 103

Original Poster
Rep: Reputation: 15
yes the branch is always 6 digit and the PCs are two digits ....the region isa always 3 digits
 
Old 08-08-2008, 01:43 AM   #7
B-Boy
Member
 
Registered: Jan 2008
Posts: 103

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by chrism01 View Post
As Mr C said, if its a fixed format you could use cut:

echo usa12312301|cut -c4-9
123123
thanks this is exactly what i am looking for :-)
 
Old 08-08-2008, 02:17 AM   #8
B-Boy
Member
 
Registered: Jan 2008
Posts: 103

Original Poster
Rep: Reputation: 15
last thing the problem i am getting now is that it cuts the character correctly but only on the first string .... usa12312301 after running echo usa12312301|cut -c4-9 i get 123123 but my file has many strings usa12312301 usa12355502 usa1236673 now it only displays the first one sting 123123 i want the cmd to work on each sting in a file and echo all
 
Old 08-08-2008, 02:35 AM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Now you tell me ... just put it in a loop. ie assuming the values are space or newline separated:
Code:
for rec in `cat file`
do
    echo $rec|cut -c4-9
done
That should get you started.
See
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 08-08-2008, 02:38 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
then use awk
Code:
awk '{
 for ( i=1 ; i<=NF; i++ ) {
  print substr($i,4,5)
 }
}
' file
 
Old 08-08-2008, 03:47 AM   #11
B-Boy
Member
 
Registered: Jan 2008
Posts: 103

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by chrism01 View Post
Now you tell me ... just put it in a loop. ie assuming the values are space or newline separated:
Code:
for rec in `cat file`
do
    echo $rec|cut -c4-9
done
That should get you started.
See
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
thanks guys...that helped
 
  


Reply



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
using sed to remove word wrap Harpune Linux - Software 2 03-02-2009 06:53 PM
remove part of string in bash script crewblunts Programming 2 03-16-2006 05:54 PM
How to remove the first part of lines? daYz Linux - Software 3 11-26-2005 06:09 PM
ispell - remove word from dictionary? TangentSpace Linux - Software 0 10-19-2005 03:04 PM
remove color after word search in vi editor dylan912 Linux - Newbie 1 12-10-2004 04:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:58 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
Open Source Consulting | Domain Registration