A clever awk command can make it easy to remove duplicate characters from a string. Credit: andy.brandon50 The awk command can make it easy to remove duplicate characters from a string even when those characters aren’t sequential, especially when the process is turned into a script. First, the awk command that we’ll be using starts by running through each letter in the string. In a more common command, you might see awk doing something like this: $ echo one:two:three | awk ‘BEGIN {FS =":"} ; { print $2 }’ two The FS portion of that command specifies the field separator—the character that is used to separate the fields in the string so that they can be processed separately. What our script does, however, is use a field separator of “” (i.e., no character). This tells awk that there are no field separators. In other words, every character is treated as if it is itself a field. Here’s are a couple examples: $ echo one:two:three | awk ‘BEGIN { FS ="" } ; { print $2 }’ n $ echo one:two:three | awk ‘BEGIN { FS ="" } ; { print $4 }’ : Note that the commands above end up displaying the second and fourth characters in the string, not the second and fourth “fields” and that no distinction is made between blanks, letters and various punctuation characters. A bash script that uses awk to remove duplicate characters might look like this: #!bin/bash echo -n “Enter string: “ read string awk -v FS="" ‘{ for(i=1;i That script prompts for a string and then uses awk to run through it one character at a time. It adds each successive character to the string (str) only if that character isn’t already included. The characters are otherwise left in their original positions, with no sorting or further processing. Here’s an example of running it: $ ./rmdups Enter string: Let’s go fly a kite! Let’s goflyaki! Notice that each character appears only once in the “Let’s goflyaki!” results. The final result of the process is displayed in the print statement in the END portion of the awk command. If you want to see how the script works by viewing the string of characters growing as characters are added, you can use this version of the script instead: #!/bin/bash echo -n “Enter string: “ read characters awk -v FS="" ‘{ for(i=1;i# } } END {print str}’ Running the script with the extra print command, you would see output like this: $ ./rmdups2 Enter string: Let’s go fly a kite! L Le Let Let’ Let’s Let’s Let’s g Let’s go Let’s go Let’s gof Let’s gofl Let’s gofly Let’s gofly Let’s goflya Let’s goflya Let’s goflyak Let’s goflyaki Let’s goflyaki Let’s goflyaki Let’s goflyaki! Let’s goflyaki! Notice that the string grows only when the current character is not already included in the string. You could also implement the script simply as an awk script like this: awk -v FS="" ‘{ for(i=1;i You could then run the awk script like this: $ echo “Let’s go fly a kite!” | rmdups.awk Let’s goflyaki! Wrap-Up Whenever processing duplicated characters more than once would be a serious waste of processing power, an awk command like that shown in this post can remove them quite easily. Related content how-to How to examine files on Linux Linux provides very useful options for viewing file attributes, such as owners and permissions, as well as file content. By Sandra Henry Stocker Oct 24, 2024 6 mins Linux how-to 8 easy ways to reuse commands on Linux Typing the same command again and again can become tiresome. Here are a number of ways you can make repeating commands – or repeating commands but with some changes – a lot easier than you might expect. By Sandra Henry-Stocker Oct 15, 2024 5 mins Linux news SUSE Edge upgrade targets Kubernetes and Linux at the edge SUSE Edge 3.1 includes a new stack validation framework and an image builder tool that are aimed at improving the scalability and manageability of complex Kubernetes and Linux edge-computing deployments. By Sean Michael Kerner Oct 15, 2024 6 mins Edge Computing Linux Network Management Software how-to Lesser-known xargs command is a versatile time saver Boost your Linux command line options and simplify your work with xargs, a handy tool for a number of data manipulation tasks. By Sandra Henry Stocker Oct 11, 2024 6 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe