← Back to Bash Course | Chapter 11: Process Management | Lesson 9 of 12

uniq कमांड

uniq एक teacher जैसा है जो एक list में साथ-साथ बैठे repeated names को cross out करता है। यह सिर्फ उन repeats को spot करता है जो एक-दूसरे के बगल में होते हैं, इसलिए आप पहले sort करते हैं।

In this page:

  1. uniq
Syntax
bash
sort file_name | uniq [-c]

uniq

uniq sorted input से consecutive duplicate lines हटाता है, इसलिए यह almost हमेशा एक pipeline में sort के बाद उपयोग होता है। -c हर line के पहले यह count prefix करता है कि यह कितनी बार occur हुई।

पहले sort किए बिना, uniq उन duplicates को miss कर देगा जो adjacent नहीं हैं।

Warning: uniq सिर्फ *adjacent* duplicate lines हटाता है — अगर duplicates एक-दूसरे के बगल में न हों तो हमेशा पहले sort करें।

उदाहरण: uniq

bash
#!/bin/bash
printf "apple\nbanana\napple\napple\ncherry\n" | sort | uniq
echo "---"
printf "apple\nbanana\napple\napple\ncherry\n" | sort | uniq -c
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. sort के बिना uniq उपयोग करना, क्योंकि यह सिर्फ adjacent duplicates हटाता है।
  2. Counts चाहिए होने पर -c भूल जाना।
  3. यह उम्मीद करना कि uniq बिना -i के case ignore करेगा।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.