DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 4th September 2008
graudeejs's Avatar
graudeejs graudeejs is offline
Real Name: Aldis Berjoza
ISO Quartermaster
 
Join Date: Jul 2008
Location: Riga, Latvia
Posts: 589
Default shell: how to take part of the line...

Example:
ThisIsMyDumbTextLine

How can i take part of text out of it....? (can't really express my self)
1) Lets say i want to get last/first 5 characters of text...
2) 3 characters starting from 4th character.....


In basic language there is function mid$(string$, offest%, lenght%)
what would be similar function in sh?

Also how can i determine how long is text line? ( len(string$) in basic)
Reply With Quote
  #2   (View Single Post)  
Old 4th September 2008
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,125
Default

See the expr(1) man page
Code:
$ text=ThisIsMyDumbTextLine
$ expr $text : '\(.....\)'
ThisI

$ expr $text : '.*\(.....\)$'
tLine

$ expr $text : '.*'
20
The Midstring function:
Code:
$ text='1234567890'
$ expr $text : '.\{4\}\(.\{5\}\)' 
56789.
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump

Last edited by J65nko; 4th September 2008 at 10:19 PM. Reason: Midstring equivalent added
Reply With Quote
  #3   (View Single Post)  
Old 4th September 2008
vermaden's Avatar
vermaden vermaden is offline
Administrator
 
Join Date: Apr 2008
Location: pl_PL.lodz
Posts: 1,056
Default

Quote:
1) Lets say i want to get last/first 5 characters of text...
2) 3 characters starting from 4th character.....
echo text | cut -c4-7

Quote:
In basic language there is function mid$(string$, offest%, lenght%)
awk
__________________
religions, worst damnation of mankind
"If 386BSD had been available when I started on Linux, Linux would probably never had happened." Linus Torvalds

Linux is not UNIX! Face it! It is not an insult. It is fact: GNU is a recursive acronym for “GNU's Not UNIX”.
vermaden's: links resources deviantart spreadbsd
Reply With Quote
  #4   (View Single Post)  
Old 4th September 2008
anomie's Avatar
anomie anomie is offline
Local
 
Join Date: Apr 2008
Location: Texas
Posts: 445
Default

This is one of my favorite (g)awk references: http://www.gnu.org/software/gawk/man...ode/index.html

See String Manipulation Functions.
__________________
Kill your t.v.
Reply With Quote
  #5   (View Single Post)  
Old 5th September 2008
anemos's Avatar
anemos anemos is offline
Fdisk Soldier
 
Join Date: May 2008
Location: Ελλάδα
Posts: 53
Default

Code:
echo texttodisplay | awk  '{print substr($1,1,1)}'
Reply With Quote
  #6   (View Single Post)  
Old 5th September 2008
graudeejs's Avatar
graudeejs graudeejs is offline
Real Name: Aldis Berjoza
ISO Quartermaster
 
Join Date: Jul 2008
Location: Riga, Latvia
Posts: 589
Default

Thanks to all of you....
Reply With Quote
  #7   (View Single Post)  
Old 5th September 2008
Carpetsmoker's Avatar
Carpetsmoker Carpetsmoker is offline
Real Name: Martin
Tcpdump Spy
 
Join Date: Apr 2008
Location: Netherlands
Posts: 2,243
Default

You can also use head and tail with the -c switch.
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things.
Reply With Quote
  #8   (View Single Post)  
Old 6th September 2008
drl's Avatar
drl drl is offline
Port Guard
 
Join Date: May 2008
Posts: 19
Default

Hi.

Some shells have this built-in for variables:
Code:
#!/bin/bash -

# @(#) s1       Demonstrate bash substring expansion.

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1)
set -o nounset

echo
echo " Results:"
t="0123456789abcdefghij"

echo
echo $t
echo

i=0
echo " First 5 ${t:$i:5}"
echo
i=-5
echo " Last  5 ${t:$i:5}"
echo
i=3
echo " 3 from 4 ${t:$i:3}"

exit 0
Producing:
Code:
% ./s1

(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0

 Results:

0123456789abcdefghij

 First 5 01234

 Last  5 fghij

 3 from 4 345
cheers, drl
Reply With Quote
  #9   (View Single Post)  
Old 6th September 2008
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Default

A thousand and one different ways, one thing you might consider is, how easy can you remember it; without having to pick apart the script later when you haven't looked at it in the last 9 months.
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote
Reply

Tags
awk, cut, expr, string manipulation, substring

Thread Tools
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
command-line-fu (digg like site with UNIX commands) vermaden Guides 3 13th March 2009 03:56 PM
avidemux2 command line roddierod FreeBSD General 1 10th November 2008 08:27 PM
OSS , playing a sound via command line.. scotsman FreeBSD General 7 29th August 2008 08:01 PM
TCSH - getting to start of line maxrussell FreeBSD General 3 4th July 2008 11:55 AM
Trouble burning to dvdrw part 2 whispersGhost Solaris 32 3rd June 2008 12:51 PM


All times are GMT. The time now is 09:13 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick