LinuxQuestions.org
Review your favorite Linux distribution.
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 12-14-2006, 09:32 AM   #1
Byenary
Member
 
Registered: Oct 2003
Distribution: edgy dapper feisty
Posts: 36

Rep: Reputation: 15
how to get a piece of text in bash


#!/bin/sh
TEXT = this is a piec of text formed bij a formail instruction


I need an instruction to grab the first 4 characters from the text, anybody knows it ?
 
Old 12-14-2006, 09:39 AM   #2
Wells
Member
 
Registered: Nov 2004
Location: Florida, USA
Distribution: Debian, Redhat
Posts: 417

Rep: Reputation: 53
The way I read this, you are trying to pull the word "this" from that text string. One way to do this is with awk:

#!/bin/sh
TEXT = this is a piece of text formed by a formall instruction
FIRSTWORD = `echo $TEXT | awk {'print $1'}`

echo $FIRSTWORD
 
Old 12-14-2006, 10:09 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
in bash, you can do string manipulation

eg

echo ${TEXT:0:4}

see http://www.faqs.org/docs/abs/HTML/st...ipulation.html
for more
 
Old 12-14-2006, 12:50 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
There are several ways:
Code:
#!/bin/bash

TEXT="This is my test string"
echo "using awk:                        $(echo "$TEXT" |awk '{ print $1 }')"
echo "using perl:                       $(echo "$TEXT" | perl -ne 's/ .*$//; print;')"
echo "using cut:                        $(echo "$TEXT" |cut -d' ' -f1)"
echo "using sed:                        $(echo "$TEXT" |sed 's/ .*//')"
echo "using expr (need to know length): $(expr substr "$TEXT" 1 4)"

# These are faster since there is no external program invocation:
echo "using shell suffix removal:       ${TEXT%% *}"
echo "using bash's substring expansion: ${TEXT:0:4}"
 
Old 12-14-2006, 02:09 PM   #5
Byenary
Member
 
Registered: Oct 2003
Distribution: edgy dapper feisty
Posts: 36

Original Poster
Rep: Reputation: 15
Thumbs up i'll use the substring thanx

In my case the substring looks like the best solution, thx for the support !!
 
  


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
After a piece of software/a bash script to mass-move files [Fedora Core 6] Alux Linux - Desktop 3 11-23-2006 02:33 PM
how to change some text of a certain line of a text file with bash and *nix scripting alred Programming 6 07-10-2006 11:55 AM
building shell command piece by piece aunquarra Linux - General 7 05-02-2006 06:43 AM
Unusual piece of bash script code raypen Slackware 2 03-18-2006 10:49 PM
retrievin a certain piece of text rharris72 Programming 3 11-22-2005 05:41 PM

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

All times are GMT -5. The time now is 11:49 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