Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Linux and Unix
Tag Cloud
access acer asus bios bsod computer crash desktop driver drivers error ethernet excel freeze gaming hard drive hardware hdmi internet laptop malware memory modem monitor motherboard mouse network printer problem ram registry repair router slow software sound trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Operating Systems > Linux and Unix >
extract characters from a variable

Reply  
Thread Tools
mgonzalo's Avatar
Junior Member with 15 posts.
 
Join Date: Apr 2007
Experience: Beginner
21-Jul-2010, 09:54 AM #1
extract characters from a variable
have the following line that I got doing a foreach var (`cat $in /l_bb`)
/ Files/project00001/data100/TestCase/G40_B_2200_pow102_pcm04.inp and I want to get character column 44-61

2200_pow102_pcm04

How do I do?
Forgive for my English I am Spanish
lotuseclat79's Avatar
Distinguished Member with 21,345 posts.
 
Join Date: Sep 2003
Location: -71.45091, 42.27841
21-Jul-2010, 12:18 PM #2
Hi mgonzalo,

If the input string/line you mean is "/ Files/project00001/data100/TestCase/G40_B_2200_pow102_pcm04.inp" and you want to extract the substring "2200_pow102_pcm04" then you mean character columns 45-61.

Two commands you might explore/play with are: cut and nawk (or gawk or awk).

Read the man pages for each command by issuing the man command as follows:
$ man cut
$ man nawk

-- Tom
__________________
The independence created by philosophical insight is - in my opinion - the mark of distinction
between a mere artisan or specialist and a real seeker after truth. - Einstein 1944
Imagination is more important than knowledge. - Einstein
Squashman's Avatar
Trusted Advisor with 18,706 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
21-Jul-2010, 01:50 PM #3
Cut should be able to do this with ease.
ghostdog74's Avatar
Member with 146 posts.
 
Join Date: Dec 2005
22-Jul-2010, 02:00 AM #4
if you are using bash, no need for external commands
Code:
$ string="/Files/project00001/data100/TestCase/G40_B_2200_pow102_pcm04.inp"
$ echo ${string:43:17}
2200_pow102_pcm04
mgonzalo's Avatar
Junior Member with 15 posts.
 
Join Date: Apr 2007
Experience: Beginner
22-Jul-2010, 09:13 AM #5
I'm using csh and I need it within a script.
I think it might be worth it to ghostdog74
I'll try
var = $(string: 43:17)
lotuseclat79's Avatar
Distinguished Member with 21,345 posts.
 
Join Date: Sep 2003
Location: -71.45091, 42.27841
22-Jul-2010, 11:03 AM #6
Hi mgonzalo,

Your use of parenthese, i.e. '(' and ')' won't work, i.e. you need to copy exactly what ghostdog74 posted, i.e. stick to using the curly braces in the assignment expression to var, i.e. '{' and '}', as in the following also using back quotes around the expression assignment:
var=`echo ${string: 43:17}`
echo $var

-- Tom
__________________
The independence created by philosophical insight is - in my opinion - the mark of distinction
between a mere artisan or specialist and a real seeker after truth. - Einstein 1944
Imagination is more important than knowledge. - Einstein
mgonzalo's Avatar
Junior Member with 15 posts.
 
Join Date: Apr 2007
Experience: Beginner
28-Jul-2010, 07:47 AM #7
Thanks tom, that was what I needed
Reply

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools


Similar Threads
Title Thread Starter Forum Replies Last Post
Solved: Excel 2007 - Formula for Extracting Specific Characters from Cell Stisfa Business Applications 7 08-Feb-2010 05:50 PM
Batch Script - Variable issue from parsing an input file Vicrater Software Development 1 02-Dec-2009 07:09 PM
php: can't pass variable from while loop through browser twmprys Web Design & Development 8 05-Nov-2009 05:07 AM
Solved: How to extract files from an old (removed) Hard Drive LarM Hardware 5 26-Mar-2009 08:48 AM
removing directories from PATH variable chucker8 Linux and Unix 1 17-Jan-2006 01:10 PM


Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 12:42 AM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.