- Joined
- Oct 10, 2018
- Posts
- 3,291
- Solutions
- 9
- Reaction
- 37,843
- Points
- 4,499
Hello, I'm kinda bored also no one about bash scripting here. so yeah. Lets Start...
How to get rid of all Tabs/or all unwanted space on a string?
Input File:
foo.txt
Bash:
foo
foo
Code:
sed -i 's|[[:blank:]]||g' foo.txt
^ stands for matching position just before the first character of the string which is basically a Regular Expression (Regex)
[[:blank:]] is basicaly a Bracket expression that aim for blank spaces and tab
"s|" & "|g" purpose of these two is to search for words or expressions (regex) and replace it...
ex. 's|oldword|newword|g'... in this case we want to delete all blanks so i leave it blank to delete it.
-i is to edit files in place..
Output:
Bash:
foo
foo
How to get the content on a specific line in file
Input File:
bar.txt
Bash:
1 Lmao
2 Lol
3 Foo
4 Bar
5 Alex
6 Steve
7 Juan
8 Erick
Code:
sed -n '4'p bar.txt
-n stands for automatic supress printing of pattern space
'<line number>' this is where you put the line number
p is to print the specific line you put.
Alternative Method:
Using AWK:
Code:
awk 'NR== 4' bar.txt
NR refers to the total record number, This is the number of input records (based on AWK's Man)
== is comparison operator which is commonly used in Conditions like (If, While, For, etc.)
4 yep its so obvious right?..
Output:
Bash:
Bar
So that's pretty much it, also i will create more threads about bash scripting.. Mag request lang kayo kung ano gusto nyo, kahit medyo complex payan OK lng.
Hope it helps :>
- PHC-TheGlock