Overclock.net › Forums › Software, Programming and Coding › Operating Systems › Linux, Unix › Bash Script - update existing lines
New Posts  All Forums:Forum Nav:

Bash Script - update existing lines

post #1 of 11
Thread Starter 
I have a script that generates a decent amount of output, but each line of output usually only relates back to 3 or 4 points of interest.

Is there a way within bash that would be able to update different, already existing lines in STDOUT?

For example, suppose I have this being displayed currently:
Code:
Line 1: AAA
Line 2: BBB
Line 3: CCC

And let's say a variable changes that would change the output of Line 2 to "DDD". Is there a way to have STDOUT update Line 2 without having the re-print Lines 1 and 3, so it would immediately change to this:
Code:
Line 1: AAA
Line 2: DDD
Line 3: CCC

Instead of:
Code:
Line 1: AAA
Line 2: BBB
Line 3: CCC

<prompt> #
Line 1: AAA
Line 2: DDD
Line 3: CCC
    
CPUMotherboardGraphicsRAM
Core i7 970 @ 4.0 GHz 1.22 Vcore Asus Rampage II Gene GTX 260 216SP G.SKILL PI 3x2gb DDR3 1600 @ 7-8-7-24 
Hard DriveOSMonitorPower
2x 500gb Seagates RAID 0, 1x 500gb non-RAID Windows 7 Professional x64 ASUS 24'' VH242H / Spectre 24'' WS Corsair 750TX 
Case
Corsair 300R 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Core i7 970 @ 4.0 GHz 1.22 Vcore Asus Rampage II Gene GTX 260 216SP G.SKILL PI 3x2gb DDR3 1600 @ 7-8-7-24 
Hard DriveOSMonitorPower
2x 500gb Seagates RAID 0, 1x 500gb non-RAID Windows 7 Professional x64 ASUS 24'' VH242H / Spectre 24'' WS Corsair 750TX 
Case
Corsair 300R 
  hide details  
Reply
post #2 of 11
yeah, it's something like:
Code:
sed 's/regex_search/regex_replace/'

But you'll need to look up the full syntax of sed (I can never remember sed's switches off by heart)

However the hardest part of sed is the regex, so if you have already learned how to do a search/replace in regex then you'll be flying.
post #3 of 11
    
CPUMotherboardGraphicsRAM
AMD Phenom II X2 550 M4N78 SE NVIDIA GeForce GTS 250 G. Skill 
Hard DriveCoolingMonitorKeyboard
WD Caviar Black 750GB Scythe Mugen 3 Samsung SyncMaster P2370 Razer Lycosa 
PowerCaseMouse
Seasonic M12II Bronze 650W Generic beige server case, on wheels. Mad Catz R.A.T. 5 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
AMD Phenom II X2 550 M4N78 SE NVIDIA GeForce GTS 250 G. Skill 
Hard DriveCoolingMonitorKeyboard
WD Caviar Black 750GB Scythe Mugen 3 Samsung SyncMaster P2370 Razer Lycosa 
PowerCaseMouse
Seasonic M12II Bronze 650W Generic beige server case, on wheels. Mad Catz R.A.T. 5 
  hide details  
Reply
post #4 of 11
Quote:
Originally Posted by xzarth View Post

This should help you: http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html

I don't think that's what he was asking for. I think (and I could be wrong here), that he wanted something that worked like a search and replace in text editors, but to work against piped output.

eg
Code:
./some_program | grep "some output" | replace "search" "replace"

[edit]

it seams replace is actually a genuine command on my Arch box.

You learn something new every day
Edited by Plan9 - 2/14/13 at 1:35am
post #5 of 11
Quote:
Originally Posted by Plan9 View Post

I don't think that's what he was asking for. I think (and I could be wrong here), that he wanted something that worked like a search and replace in text editors, but to work against piped output.

eg
Code:
./some_program | grep "some output" | replace "search" "replace"

[edit]

it seams replace is actually a genuine command on my Arch box.

You learn something new every day

i'll let the original poster respond, but for me the line:
Code:
<prompt> #
in his first post is a dead giveaway that he means on screen, not in a file.

Well, we'll see.
    
CPUMotherboardGraphicsRAM
AMD Phenom II X2 550 M4N78 SE NVIDIA GeForce GTS 250 G. Skill 
Hard DriveCoolingMonitorKeyboard
WD Caviar Black 750GB Scythe Mugen 3 Samsung SyncMaster P2370 Razer Lycosa 
PowerCaseMouse
Seasonic M12II Bronze 650W Generic beige server case, on wheels. Mad Catz R.A.T. 5 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
AMD Phenom II X2 550 M4N78 SE NVIDIA GeForce GTS 250 G. Skill 
Hard DriveCoolingMonitorKeyboard
WD Caviar Black 750GB Scythe Mugen 3 Samsung SyncMaster P2370 Razer Lycosa 
PowerCaseMouse
Seasonic M12II Bronze 650W Generic beige server case, on wheels. Mad Catz R.A.T. 5 
  hide details  
Reply
post #6 of 11
Quote:
Originally Posted by xzarth View Post

i'll let the original poster respond, but for me the line:
Code:
<prompt> #
in his first post is a dead giveaway that he means on screen, not in a file.

Well, we'll see.

screen / file - it's the same thing when talking about STDOUT. But I take your point about <prompt>, I did miss that smile.gif

[edit]

ignore that. I was still missing your point there laugher.gif
Edited by Plan9 - 2/14/13 at 9:54am
post #7 of 11
Thread Starter 
Quote:
Originally Posted by xzarth View Post

i'll let the original poster respond, but for me the line:
Code:
<prompt> #
in his first post is a dead giveaway that he means on screen, not in a file.

Well, we'll see.

Correct. I could have worded that better, though.

As an example, when running testdisk or photorec, the current working sector position just updates in place on screen without printing a new line. Now, I realize photorec is not written in Bash, I'm just wondering if such a functionality exists for bash.
    
CPUMotherboardGraphicsRAM
Core i7 970 @ 4.0 GHz 1.22 Vcore Asus Rampage II Gene GTX 260 216SP G.SKILL PI 3x2gb DDR3 1600 @ 7-8-7-24 
Hard DriveOSMonitorPower
2x 500gb Seagates RAID 0, 1x 500gb non-RAID Windows 7 Professional x64 ASUS 24'' VH242H / Spectre 24'' WS Corsair 750TX 
Case
Corsair 300R 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Core i7 970 @ 4.0 GHz 1.22 Vcore Asus Rampage II Gene GTX 260 216SP G.SKILL PI 3x2gb DDR3 1600 @ 7-8-7-24 
Hard DriveOSMonitorPower
2x 500gb Seagates RAID 0, 1x 500gb non-RAID Windows 7 Professional x64 ASUS 24'' VH242H / Spectre 24'' WS Corsair 750TX 
Case
Corsair 300R 
  hide details  
Reply
post #8 of 11
Quote:
Originally Posted by TurboTurtle View Post

Correct. I could have worded that better, though.

As an example, when running testdisk or photorec, the current working sector position just updates in place on screen without printing a new line. Now, I realize photorec is not written in Bash, I'm just wondering if such a functionality exists for bash.

You're much better off doing this in Perl using ncurses.

Have you done much (any) Perl before? Or any other scripting language?
post #9 of 11
Thread Starter 
Quote:
Originally Posted by Plan9 View Post

You're much better off doing this in Perl using ncurses.

Have you done much (any) Perl before? Or any other scripting language?

Bit of Python, but I'm waaaaay rusty, hence my preference for bash if it were possible.

Haven't dabbled in Perl yet, though.

Looks like it's time to dig through the closet-o-stuff for my old books.
    
CPUMotherboardGraphicsRAM
Core i7 970 @ 4.0 GHz 1.22 Vcore Asus Rampage II Gene GTX 260 216SP G.SKILL PI 3x2gb DDR3 1600 @ 7-8-7-24 
Hard DriveOSMonitorPower
2x 500gb Seagates RAID 0, 1x 500gb non-RAID Windows 7 Professional x64 ASUS 24'' VH242H / Spectre 24'' WS Corsair 750TX 
Case
Corsair 300R 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Core i7 970 @ 4.0 GHz 1.22 Vcore Asus Rampage II Gene GTX 260 216SP G.SKILL PI 3x2gb DDR3 1600 @ 7-8-7-24 
Hard DriveOSMonitorPower
2x 500gb Seagates RAID 0, 1x 500gb non-RAID Windows 7 Professional x64 ASUS 24'' VH242H / Spectre 24'' WS Corsair 750TX 
Case
Corsair 300R 
  hide details  
Reply
post #10 of 11
I'm not sure I completely understand your problem ... but you can use a hash in awk to group output to unique lines only ... perhaps similar to what you want, idk
Code:
echo -e "1\n2\n3\n4\n1\n2" | awk ' { if (map[$0] == "")  { print; map[$0] = $0; }  } '
1
2
3
4

hope that helps

but I think maybe you talking about rolling the cursor back and writing over top of existing output??
stable again
(25 items)
 
  
CPUCPUMotherboardGraphics
E5-2687W E5-2687W ASUS Z9PED8-WS EVGA GTX 570 (Linux host) 
GraphicsRAMHard DriveHard Drive
EVGA GTX 970 FTW (win7 guest) 64GB G.SKILL 2133 2x Crucial M4 256GB raid1 4x 3TB raid 10 
CoolingCoolingCoolingCooling
2x Apogee HD  2x RX 480 2x MCP 655 RP-452x2 rev2 (new) 
CoolingCoolingOSOS
16x Cougar Turbine CFT12SB4 (new) EK FC 580 Gentoo (host) Gentoo (x23 guests) 
OSMonitorMonitorPower
windows 7 (guest w/ vfio-pci) Viewsonic 23" 1080P Viewsonic 19" Antec HCP Platinum 1000 (new) 
CaseOtherOther
Case Labs TH10 (still the best ever) 2x Lamptron FC-5 IOGEAR 2 way DVI KVM Switch 
  hide details  
Reply
stable again
(25 items)
 
  
CPUCPUMotherboardGraphics
E5-2687W E5-2687W ASUS Z9PED8-WS EVGA GTX 570 (Linux host) 
GraphicsRAMHard DriveHard Drive
EVGA GTX 970 FTW (win7 guest) 64GB G.SKILL 2133 2x Crucial M4 256GB raid1 4x 3TB raid 10 
CoolingCoolingCoolingCooling
2x Apogee HD  2x RX 480 2x MCP 655 RP-452x2 rev2 (new) 
CoolingCoolingOSOS
16x Cougar Turbine CFT12SB4 (new) EK FC 580 Gentoo (host) Gentoo (x23 guests) 
OSMonitorMonitorPower
windows 7 (guest w/ vfio-pci) Viewsonic 23" 1080P Viewsonic 19" Antec HCP Platinum 1000 (new) 
CaseOtherOther
Case Labs TH10 (still the best ever) 2x Lamptron FC-5 IOGEAR 2 way DVI KVM Switch 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Linux, Unix
Overclock.net › Forums › Software, Programming and Coding › Operating Systems › Linux, Unix › Bash Script - update existing lines