Reverse a String | Shell Programming
Last Updated :
18 Oct, 2024
In shell scripting, reversing a string can be done using various methods, such as rev, awk, sed, and Perl. Here, we provide examples of reversing a string using different approaches and commands in Unix/Linux systems. We are given a string and we have to use shell script to print it in the reverse order.
Syntax
The general syntax varies depending on the command used for reversing the string.
Using rev command:
echo "string" | rev
Using awk command:
echo "string" | awk '{ for(i = length; i!=0; i--) x = x substr($0, i, 1);} END {print x}'
Using sed and tac:
echo "string" | sed 's/./&\n/g' | tac | sed -e :a -e 'N;s/\n//g;ta'
Using perl:
echo "string" | perl -ne 'chomp; print scalar reverse;'
Key Methods for Reversing Strings
Asked in FICO Examples:
Input : geeksforgeeks
Output :skeegrofskeeg
Algorithm
step 1:- Take user input in a string
step 2:- Findthe length of given string using length function
step 3:- Set i = length-1 and run loop till i <= 0
step 4:- echo the $i
step 5:- repeat step 3 and 4 till i==0
step 6:- end
CPP
// reverse a string using shell script
// reverse a string is in linux and unix
#!/ bin / bash
// reading a string
// using via user input
read - p "Enter string:" string
// getting the length of given string
len
= $
{
#string
}
// looping for reversing a string
// initialize i=len-1 for reversing a string and run till i=0
// printing in the reverse order of the given string
for ((i = $len - 1; i >= 0; i--))
do
// "${string:$i:1}"extract single character from string.
reverse = "$reverse${string:$i:1}" done
echo "$reverse"
Output:
skeegrofskeeg
Reverse a string in shell scripting using Commands
Method 1: Using rev Command
The rev command reverses the lines in a file or from standard input.
rev command: It is used to reverse the lines in a file. This command can take standard input as well as shown below.
$ echo welcome | rev
emoclew
Note: The rev command is not present in all flavors of Unix.
Method 2: Using awk Command
Using awk to reverse a string by iterating over each character from the end and appending it.
awk command: Using the substring function, can reverse a string
$ echo welcome | awk
'{ for(i = length; i!=0; i--)
x = x substr($0, i, 1);
}END
{print x}'
emoclew
This logic is the common logic used in any programming language to reverse a string: Start from the last position of the string and keep printing a character till the first position is reached. We loop on the string starting from the last position and moving towards the beginning.
The length command gives the length of the argument passed to it. With no argument, length gives the length of the current line which is $0. The substr command in awk extracts one character at a time and is appended to the resultant variable x which is printed at the end using the END label.
Method 3: Using sed and tac Commands
This method uses sed to append a newline after every character, tac to reverse the lines, and sed again to join them.
sed command: Total 2 sed commands are used here.
$ echo welcome | sed 's/./&\n/g' | tac | sed -e :a -e 'N;s/\n//g;ta'
emoclew
To understand this command properly, break it up at every pipe and observe the output. The first sed reads a character(.) and appends a newline(\n) character behind every matched character(&). tac command reverses the contents of a file or the standard input. The second sed command now joins all the lines together. Note: The tac command is not present in all the Unix flavors.
Method 4: Using perl
The perl solution uses the built-in reverse function to reverse the string.
Perl solution: Using the inbuilt perl function reverse.
$ echo welcome | perl -ne 'chomp;
print scalar reverse;'
emoclew
reverse function reverses all the elements present in a list. In scalar context as used here, it reverses a string as well. The chomp command is used to remove the newline character in the string.
Conclusion
Reversing a string in shell programming can be done using a variety of commands such as rev, awk, sed, or perl. Each method has its own use cases, with some commands being more readily available on certain systems.
Similar Reads
Remove Last character from String in Linux In this article, we will discuss how to remove the last character from the string in Linux. In Linux, there are various commands and techniques present by which you can do this task in an easier way if you have some basic knowledge about the commands in Linux. Here, you will see the different comman
3 min read
Batch Script - Right String In this article, we are going to learn how to use the concept of Right String using Batch Script. Right String uses the concept of negative indexing. We have to extract the characters using :~ followed by the negative index of the character from which we want to print a substring from the main strin
2 min read
Shell Program to Find the Position of Substring in Given String A string is made of many substrings or can say that if we delete one or more characters from the beginning or end then the remaining string is called substring. This article is about to write a shell program that will tell the position (index) of the substring in a given string. Let's take an exampl
7 min read
rev command in Linux with Examples rev command in Linux is used to reverse the lines characterwise. This utility reverses the order of the characters in each line by copying the specified files to the standard output. If no files are specified, then the standard input will be read. Here, we will explore the syntax, examples, and opti
3 min read
tac command in Linux with Examples tac command in Linux is used to concatenate and print files in reverse. This command will write each FILE to standard output, the last line first. When no file is specified then this command will read the standard input. Here, we will look deeper into the tac command, exploring its syntax, various o
3 min read