LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-13-2008, 05:26 PM   #1
alenD
Member
 
Registered: Jul 2005
Posts: 69

Rep: Reputation: 15
bash operations with string


Hi all

I am looking for a bash script that would read a string
sdfsfsdf123
and output digits - 123
and the opposite, read the string and input digits in place of the current digits.

thanks in advance
 
Old 03-13-2008, 05:46 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
What criteria should be used to identify the 123 part? Options might be:
  • The last 3 characters of the input string
  • Only digits on the end of the string
  • All after the first digit in the string
  • Any character not in the a-z range
  • All characters after position 9 in the input string
  • Some other criteria?
 
Old 03-13-2008, 05:46 PM   #3
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
To print just the digits:
Code:
echo "sdfsdfsdf123" | sed 's/[^1-9]//g'
To change the digits to some other digits, say 567
Code:
echo "sdfssdfsdf123" | sed 's/[1-9]\+/567/g'
 
Old 03-13-2008, 05:47 PM   #4
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
Quote:
Originally Posted by matthewg42 View Post
What criteria should be used to identify the 123 part? Options might be:
  • The last 3 characters of the input string
  • Only digits on the end of the string
  • All after the first digit in the string
  • Any character not in the a-z range
  • All characters after position 9 in the input string
  • Some other criteria?
Ah, very true. I was just assuming you meant any string of numbers. That's what my code does.
 
Old 03-13-2008, 09:32 PM   #5
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
thanks to your request i got fun again writing these new scripts

(a)
Code:
#!/bin/bash

echo -n "input new string with numbers: "
read string

# get the digits, there are many ways to do this
digits=$(echo -n "$string" | sed 's/[^1-9]//g')

# count the number of digits
dcount=$(echo -n "$digits" | wc -c)

# check digit count
if [[ dcount -eq 0 ]]; then
	echo "there where no digit found in the string"
	exit 1
fi

# print the digits
echo "digits found: $digits"

# ask for a replacement
until echo -n "please enter a $dcount digit number to replace the digits: ";
read rdigits;
[ ${#rdigits} -eq $dcount ] && [[ $rdigits =~ ^[[:digit:]]+$ ]]; do
	echo "that's not a 3 digit number!"
done

# split the strings to arrays
stringarray=( $(echo "$string" | sed 's/./& /g') )
rdigitsarray=( $(echo "$rdigits" | sed 's/./& /g') )

# print
rpos=0
for a in ${stringarray[*]}; do
	if [[ $a =~ [[:digit:]] ]]; then
		echo -n ${rdigitsarray[b++]}
	else
		echo -n ${a}
	fi
done
echo
(b)
Code:
#!/bin/bash

until

# ask for strings
echo -n "input new string with numbers: "
read string

# get the digits, there are many ways to do this
digits=$(echo -n "$string" | sed 's/[^0-9]//g')

# count the number of digits
dcount=$(echo -n "$digits" | wc -c)

# check digit count
[[ dcount -gt 0 ]]; do
	echo "there where no digit found in the string"
done

# print the digits
echo "digits found: $digits"

# ask for a replacement
until echo -n "please enter a number to replace the digits: ";
read rdigits;
[ ${#rdigits} -gt 0 ] && [[ $rdigits =~ ^[[:digit:]]+$ ]]; do
	echo "that's not a number!"
done

# split the strings to arrays
stringarray=( $(echo "$string" | sed 's/./& /g') )
rdigitsarray=( $(echo "$rdigits" | sed 's/./& /g') )

# print
rpos=0
rcount=${#rdigitsarray[@]}
spos=0
scount=${#stringarray[@]}
while [[ spos -lt scount && rpos -lt rcount && rpos -lt dcount ]]; do
	if s=${stringarray[spos++]}; [[ ${s} =~ [[:digit:]] ]]; then
		echo -n "${rdigitsarray[rpos++]}"
	else
		echo -n "${s}"
	fi
done
while [[ spos -lt scount ]]; do
	echo -n "${stringarray[spos++]}"
done
while [[ rpos -lt rcount ]]; do
	echo -n "${rdigitsarray[rpos++]}"
done
echo
The first code is strict to the actual number of digits you put in the first string

For the second one, as long as you enter a number to replace the string, it will use it.

hope this can help

regards

Edit:
btw if you want to append the extra digits of the replace string to the last digit of the first string instead of the last character, you just have to reverse the last parts of the code to:
Code:
while [[ rpos -lt rcount ]]; do
	echo -n "${rdigitsarray[rpos++]}"
done
while [[ spos -lt scount ]]; do
	echo -n "${stringarray[spos++]}"
done

Last edited by konsolebox; 03-13-2008 at 09:44 PM.
 
Old 03-17-2008, 02:37 PM   #6
alenD
Member
 
Registered: Jul 2005
Posts: 69

Original Poster
Rep: Reputation: 15
thanx! this is very helpful...
 
Old 10-17-2008, 02:43 PM   #7
ravi2ray
LQ Newbie
 
Registered: Oct 2008
Location: Kansas, MO
Posts: 9

Rep: Reputation: 0
Thank you so much. This is very helpful. I was looking for a peace of this code

Thank you so much. This is very helpful. I was looking for a peace of this code
 
  


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
cutting a string in bash freeindy Linux - General 3 03-06-2008 02:57 AM
String manipulation in bash dalmat Linux - Software 4 12-03-2007 05:50 PM
string manipulation in BASH ovince Programming 4 04-16-2007 07:15 PM
Bash way to tell if String is in String tongar Programming 3 06-16-2005 06:59 AM
file operations in BASH snorky Linux - Newbie 2 11-24-2003 07:07 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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